[英]Android development
應用程序意外停止
按下指定頁面上的按鈕時出現此錯誤。
此代碼可掛起應用程序:-代碼段
<Button android:text="Button"
android:onClick="SaveRegistration"
android:id="@+id/btnAddRegistration"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</Button>
public void SaveRegistration(View view) {
Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
}
我在2.2版上運行
Logcat錯誤日志
06-26 14:25:52.613: ERROR/AndroidRuntime(447): FATAL EXCEPTION: main
06-26 14:25:52.613: ERROR/AndroidRuntime(447): java.lang.IllegalStateException: Could not find a method add(View) in the activity class com.millerbean.gasApp.MillerbeansGasInfoActivity for onClick handler on view class android.widget.Button with id 'btnTest'
06-26 14:25:52.613: ERROR/AndroidRuntime(447): at android.view.View$1.onClick(View.java:2131)
06-26 14:25:52.613: ERROR/AndroidRuntime(447): at android.view.View.performClick(View.java:2485)
06-26 14:25:52.613: ERROR/AndroidRuntime(447): at android.view.View$PerformClick.run(View.java:9080)
06-26 14:25:52.613: ERROR/AndroidRuntime(447): at android.os.Handler.handleCallback(Handler.java:587)
06-26 14:25:52.613: ERROR/AndroidRuntime(447): at android.os.Handler.dispatchMessage(Handler.java:92)
06-26 14:25:52.613: ERROR/AndroidRuntime(447): at android.os.Looper.loop(Looper.java:123)
06-26 14:25:52.613: ERROR/AndroidRuntime(447): at android.app.ActivityThread.main(ActivityThread.java:3683)
06-26 14:25:52.613: ERROR/AndroidRuntime(447): at java.lang.reflect.Method.invokeNative(Native Method)
06-26 14:25:52.613: ERROR/AndroidRuntime(447): at java.lang.reflect.Method.invoke(Method.java:507)
06-26 14:25:52.613: ERROR/AndroidRuntime(447): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-26 14:25:52.613: ERROR/AndroidRuntime(447): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-26 14:25:52.613: ERROR/AndroidRuntime(447): at dalvik.system.NativeStart.main(Native Method)
06-26 14:25:52.613: ERROR/AndroidRuntime(447): Caused by: java.lang.NoSuchMethodException: add
06-26 14:25:52.613: ERROR/AndroidRuntime(447): at java.lang.ClassCache.findMethodByName(ClassCache.java:247)
06-26 14:25:52.613: ERROR/AndroidRuntime(447): at java.lang.Class.getMethod(Class.java:962)
06-26 14:25:52.613: ERROR/AndroidRuntime(447): at android.view.View$1.onClick(View.java:2124)
06-26 14:25:52.613: ERROR/AndroidRuntime(447): ... 11 more
06-26 14:25:52.643: WARN/ActivityManager(75): Force finishing activity com.millerbean.gasApp/.MillerbeansGasInfoActivity
06-26 14:25:53.193: WARN/ActivityManager(75): Activity pause timeout for HistoryRecord{4050f5b0 com.millerbean.gasApp/.MillerbeansGasInfoActivity}
問題解決了!
在我的主要Java文件中,我的第一個按鈕上包含以下內容:setContentView(R.layout.newregistration); 它將布局更改為另一個xml文件newregistration.xml
稍后,我創建了一個Addregistration.java文件,在其中放置了事件處理程序。 需要將事件處理程序放置在我的主Java文件中,現在它可以工作了。
之間有什么區別:1. setContentView(R.layout.newregistration); 2. startActivity(new Intent(this,MenuBuilder.class));
1.設置用戶看到的布局是否正確2.設置要獲取事件處理程序的類等,是否正確?
您的班級是否像下面那樣擴展Activity
?
從Android網站:
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("Hello, Android");
setContentView(tv);
}
}
編輯:注釋掉Toast會導致錯誤消失嗎? 如果是這樣,這可能是問題所在。 在Android教程中進行工作 。
如果刪除該怎么辦
android:onClick="SaveRegistration"
從您的布局並在您的代碼中進行操作,例如
Button myButton = this.findViewById(btnAddRegistration);
myButton.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
SaveRegistration(v);
}
}
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.