尝试在 android 上编写我的第一个应用程序。但是当单击任何按钮时,应用程序在虚拟设备上崩溃(FC)。请看一下.. MainActivity.java:
public class MainActivity extends Activity {
public EditText t1 = null, t2 = null;
Button b1 = null, b2 = null;
TextView tv1 = null,tv2 = null;
int a = 0, b = 0, S1 = 0, S2 = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1=(EditText)findViewById(R.id.editText1);
t2=(EditText)findViewById(R.id.EditText2);
b1=(Button)findViewById(R.id.button1);
b2=(Button)findViewById(R.id.button2);
tv1=(TextView)findViewById(R.id.TextView1);
tv2=(TextView)findViewById(R.id.TextView2);
}
public void onClick1()
{
tv1.clearComposingText();
a=Integer.parseInt(t1.getText().toString());
tv1.setText(S1+=a);
}
public void onClick2()
{
tv2.clearComposingText();
b=Integer.parseInt(t2.getText().toString());
tv2.setText(S2+=b);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
活动主.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" >
<Button
android:id="@+id/button1"
android:layout_width="80dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="30dp"
android:text="Add"
android:onClick="onClick1" />
<Button
android:id="@+id/button2"
android:layout_width="80dp"
android:layout_height="60dp"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_marginLeft="26dp"
android:layout_toRightOf="@+id/button1"
android:text="Add"
android:onClick="onClick2" />
<EditText
android:id="@+id/editText1"
android:layout_width="80dp"
android:layout_height="60dp"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="24dp"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/EditText2"
android:layout_width="80dp"
android:layout_height="60dp"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_alignLeft="@+id/button2"
android:ems="10" />
<TextView
android:id="@+id/TextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/editText1"
android:layout_centerVertical="true"
android:text="" />
<TextView
android:id="@+id/TextView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/TextView1"
android:layout_alignLeft="@+id/EditText2"
android:text="" />
和 logcat 日志文件:
10-14 17:36:35.714: E/AndroidRuntime(697): FATAL EXCEPTION: main
10-14 17:36:35.714: E/AndroidRuntime(697): java.lang.IllegalStateException: Could not find a method onClick1(View) in the activity class com.example.blot.MainActivity for onClick handler on view class android.widget.Button with id 'button1'
10-14 17:36:35.714: E/AndroidRuntime(697): at android.view.View$1.onClick(View.java:2131)
10-14 17:36:35.714: E/AndroidRuntime(697): at android.view.View.performClick(View.java:2485)
10-14 17:36:35.714: E/AndroidRuntime(697): at android.view.View$PerformClick.run(View.java:9080)
10-14 17:36:35.714: E/AndroidRuntime(697): at android.os.Handler.handleCallback(Handler.java:587)
10-14 17:36:35.714: E/AndroidRuntime(697): at android.os.Handler.dispatchMessage(Handler.java:92)
10-14 17:36:35.714: E/AndroidRuntime(697): at android.os.Looper.loop(Looper.java:123)
10-14 17:36:35.714: E/AndroidRuntime(697): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-14 17:36:35.714: E/AndroidRuntime(697): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 17:36:35.714: E/AndroidRuntime(697): at java.lang.reflect.Method.invoke(Method.java:507)
10-14 17:36:35.714: E/AndroidRuntime(697): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-14 17:36:35.714: E/AndroidRuntime(697): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-14 17:36:35.714: E/AndroidRuntime(697): at dalvik.system.NativeStart.main(Native Method)
10-14 17:36:35.714: E/AndroidRuntime(697): Caused by: java.lang.NoSuchMethodException: onClick1
10-14 17:36:35.714: E/AndroidRuntime(697): at java.lang.ClassCache.findMethodByName(ClassCache.java:247)
10-14 17:36:35.714: E/AndroidRuntime(697): at java.lang.Class.getMethod(Class.java:962)
10-14 17:36:35.714: E/AndroidRuntime(697): at android.view.View$1.onClick(View.java:2124)
10-14 17:36:35.714: E/AndroidRuntime(697): ... 11 more