Android开发

时间:2011-06-26 13:45:24

标签: android android-emulator

应用程序意外停止

按指定页面上的按钮时出现此错误。

此代码挂起应用程序: - 代码片段

    <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.设置哪个类获取事件处理程序等。

2 个答案:

答案 0 :(得分:0)

您的班级是否如下所示扩展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 tutorials

进行操作

答案 1 :(得分:0)

如果删除

怎么样?
android:onClick="SaveRegistration"
从您的布局

并在您的代码中执行,例如

Button myButton = this.findViewById(btnAddRegistration);
myButton.setOnClickListener(new OnClickListener()
        {
            public void onClick(View v) 
            {
                SaveRegistration(v);
            }
        }
    );