计算两个数字相加时遇到问题
MainActivity.java
public class MainActivity extends Activity {
EditText FirstNumber,SecondNumber;
Button CalculateValue;
TextView Result;
int x,y,z;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
FindAllTheViews();
CalculateValue.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
CalculateTheSum();
}
});
}
private void FindAllTheViews() {
FirstNumber=(EditText) findViewById(R.id.First_Number);
SecondNumber=(EditText) findViewById(R.id.Second_Number);
CalculateValue=(Button) findViewById(R.id.Calculate_Button);
Result=(TextView) findViewById(R.id.Final_Result);
}
private void CalculateTheSum() {
x=new Integer(FirstNumber.getText().toString());
y=new Integer(SecondNumber.getText().toString());
z=x+y;
Result.setText(z);
}
}
主要的.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="@+id/First_Number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_marginLeft="36dp"
android:layout_toRightOf="@+id/textView1"
android:ems="10"
android:hint="Enter Number" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/Second_Number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_alignLeft="@+id/First_Number"
android:ems="10"
android:hint="Enter number" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="30dp"
android:layout_toRightOf="@+id/textView1" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView3"
android:layout_alignBottom="@+id/textView3"
android:layout_centerHorizontal="true"
android:text="WELCOME"
android:textSize="30sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Second-Number" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView3"
android:layout_marginTop="46dp"
android:text="First-Number" />
<Button
android:id="@+id/Calculate_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_alignRight="@+id/Second_Number"
android:layout_below="@+id/Second_Number"
android:layout_marginTop="50dp"
android:text="Calculate" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/Final_Result"
android:text="The result is::"
android:textSize="25sp"
android:textStyle="bold|normal" />
<TextView
android:id="@+id/Final_Result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/Calculate_Button"
android:layout_below="@+id/Calculate_Button"
android:layout_marginRight="28dp"
android:layout_marginTop="45dp"
android:text="Result"
android:textSize="25sp" />
</RelativeLayout>
日志
09-11 14:46:22.794: E/AndroidRuntime(410): FATAL EXCEPTION: main
09-11 14:46:22.794: E/AndroidRuntime(410): android.content.res.Resources$NotFoundException: String resource ID #0x64
09-11 14:46:22.794: E/AndroidRuntime(410): at android.content.res.Resources.getText(Resources.java:201)
09-11 14:46:22.794: E/AndroidRuntime(410): at android.widget.TextView.setText(TextView.java:2857)
09-11 14:46:22.794: E/AndroidRuntime(410): at com.example.addingtwonumbers.MainActivity.CalculateTheSum(MainActivity.java:49)
09-11 14:46:22.794: E/AndroidRuntime(410): at com.example.addingtwonumbers.MainActivity.access$0(MainActivity.java:45)
09-11 14:46:22.794: E/AndroidRuntime(410): at com.example.addingtwonumbers.MainActivity$1.onClick(MainActivity.java:32)
09-11 14:46:22.794: E/AndroidRuntime(410): at android.view.View.performClick(View.java:2485)
09-11 14:46:22.794: E/AndroidRuntime(410): at android.view.View$PerformClick.run(View.java:9080)
09-11 14:46:22.794: E/AndroidRuntime(410): at android.os.Handler.handleCallback(Handler.java:587)
09-11 14:46:22.794: E/AndroidRuntime(410): at android.os.Handler.dispatchMessage(Handler.java:92)
09-11 14:46:22.794: E/AndroidRuntime(410): at android.os.Looper.loop(Looper.java:123)
09-11 14:46:22.794: E/AndroidRuntime(410): at android.app.ActivityThread.main(ActivityThread.java:3683)
09-11 14:46:22.794: E/AndroidRuntime(410): at java.lang.reflect.Method.invokeNative(Native Method)
09-11 14:46:22.794: E/AndroidRuntime(410): at java.lang.reflect.Method.invoke(Method.java:507)
09-11 14:46:22.794: E/AndroidRuntime(410): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-11 14:46:22.794: E/AndroidRuntime(410): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-11 14:46:22.794: E/AndroidRuntime(410): at dalvik.system.NativeStart.main(Native Method)