JAVA通用域名格式。进程在Android emulator和mobile中意外停止
我正在尝试制作一个简单的安卓应用程序,它将添加两个数字并显示输出。然而,我不断地得到以下错误。 “calculator.com.process意外停止” 我使用API 10呈现布局,使用API 17作为编译器和目标SDK
经过这么多麻烦,我终于可以发布代码了
这是主要活动的代码。爪哇
package com.example.calculator;
import 安卓.support.v7.app.ActionBarActivity;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.widget.EditText;
import 安卓.widget.TextView;
public class MainActivity extends ActionBarActivity {
EditText txtNum1;
EditText txtNum2;
TextView Display;
Button bCal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtNum1=(EditText) findViewById(R.id.Num1);
txtNum1=(EditText) findViewById(R.id.Num2);
Display=(TextView)findViewById(R.id.tvDisplay);
bCal=(Button)findViewById(R.id.bAdd);
bCal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View Tawfiq) {
// TODO Auto-generated method stub
double x=Double.parseDouble(txtNum1.getText().toString());
double y=Double.parseDouble(txtNum2.getText().toString());
double total=x+y;
Display.setText("Sum is"+total);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
xml代码:
<RelativeLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:id="@+id/bAdd"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:paddingBottom="@dimen/activity_vertical_margin"
安卓:paddingLeft="@dimen/activity_horizontal_margin"
安卓:paddingRight="@dimen/activity_horizontal_margin"
安卓:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.calculator.MainActivity" >
<EditText
安卓:id="@+id/Num1"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignParentTop="true"
安卓:layout_centerHorizontal="true"
安卓:layout_marginTop="50dp"
安卓:ems="10"
安卓:inputType="numberDecimal" >
<requestFocus />
</EditText>
<EditText
安卓:id="@+id/Num2"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_alignLeft="@+id/Num1"
安卓:layout_below="@+id/Num1"
安卓:layout_marginTop="33dp"
安卓:ems="10"
安卓:inputType="numberDecimal" />
<Button
安卓:id="@+id/bAdd"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/tvDisplay"
安卓:layout_centerHorizontal="true"
安卓:layout_marginTop="20dp"
安卓:text="Button" />
<TextView
安卓:id="@+id/tvDisplay"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:layout_below="@+id/Num2"
安卓:layout_centerHorizontal="true"
安卓:layout_marginTop="30dp"
安卓:text="Sum"
安卓:textAppearance="?安卓:attr/textAppearanceLarge" />
</RelativeLayout>
这是logcat
01-24 16:06:39.840: D/AndroidRuntime(1130): Shutting down VM
01-24 16:06:39.870: W/dalvikvm(1130): threadid=1: thread exiting with uncaught exception (group=0xb60164f0)
01-24 16:06:39.950: E/AndroidRuntime(1130): FATAL EXCEPTION: main
01-24 16:06:39.950: E/AndroidRuntime(1130): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.calculator/com.example.calculator.MainActivity}: java.lang.ClassCastException: 安卓.widget.RelativeLayout
01-24 16:06:39.950: E/AndroidRuntime(1130): at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-24 16:06:39.950: E/AndroidRuntime(1130): at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-24 16:06:39.950: E/AndroidRuntime(1130): at 安卓.app.ActivityThread.access$1500(ActivityThread.java:117)
01-24 16:06:39.950: E/AndroidRuntime(1130): at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-24 16:06:39.950: E/AndroidRuntime(1130): at 安卓.os.Handler.dispatchMessage(Handler.java:99)
01-24 16:06:39.950: E/AndroidRuntime(1130): at 安卓.os.Looper.loop(Looper.java:130)
01-24 16:06:39.950: E/AndroidRuntime(1130): at 安卓.app.ActivityThread.main(ActivityThread.java:3683)
01-24 16:06:39.950: E/AndroidRuntime(1130): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 16:06:39.950: E/AndroidRuntime(1130): at java.lang.reflect.Method.invoke(Method.java:507)
01-24 16:06:39.950: E/AndroidRuntime(1130): at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-24 16:06:39.950: E/AndroidRuntime(1130): at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-24 16:06:39.950: E/AndroidRuntime(1130): at dalvik.system.NativeStart.main(Native Method)
01-24 16:06:39.950: E/AndroidRuntime(1130): Caused by: java.lang.ClassCastException: 安卓.widget.RelativeLayout
01-24 16:06:39.950: E/AndroidRuntime(1130): at com.example.calculator.MainActivity.onCreate(MainActivity.java:27)
01-24 16:06:39.950: E/AndroidRuntime(1130): at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-24 16:06:39.950: E/AndroidRuntime(1130): at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-24 16:06:39.950: E/AndroidRuntime(1130): ... 11 more
# 1 楼答案
你在27号线遇到了问题
更改xml文件中的relativeLayouts Id。 它不应与“添加”按钮相同
如上所述进行更改。 同一布局中的两个id不能相同。 “添加”按钮和相对布局的id相同,。 在findviewbyd()期间,按钮中的相对布局是按等级排列的。 这就是为什么班级演员特例即将到来
编辑:
我编译了你的代码:- 你犯的下一个错误是——
应该是的
错误:您没有将引用指定给第二版文本