简体   繁体   English

Android开发

[英]Android development

The applications has stopped unexpectedly 应用程序意外停止

I get this error when pressing a button on a specified page. 按下指定页面上的按钮时出现此错误。

This code hangs the application: --Code snippet 此代码可挂起应用程序:-代码段

    <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();
    }

I run on version 2.2 我在2.2版上运行

Logcat errorlog 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}

Problem solved! 问题解决了!

In my main java file I have the following on my first button: setContentView(R.layout.newregistration); 在我的主要Java文件中,我的第一个按钮上包含以下内容:setContentView(R.layout.newregistration); it change the layout to another xml file newregistration.xml 它将布局更改为另一个xml文件newregistration.xml

Later on I created a Addregistration.java file where I placed the event handler. 稍后,我创建了一个Addregistration.java文件,在其中放置了事件处理程序。 The event handler needed to be placed in my main java file and now it works. 需要将事件处理程序放置在我的主Java文件中,现在它可以工作了。

What is the difference between: 1. setContentView(R.layout.newregistration); 之间有什么区别:1. setContentView(R.layout.newregistration); 2. startActivity(new Intent(this, MenuBuilder.class)); 2. startActivity(new Intent(this,MenuBuilder.class));

Is it correct that 1. set the layouts that the user see 2. set which class to get eventhandlers and etc. 1.设置用户看到的布局是否正确2.设置要获取事件处理程序的类等,是否正确?

Does your class extend Activity like below? 您的班级是否像下面那样扩展Activity

From Android site: 从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);
   }
}

Edit: Does commenting out the Toast cause the error to go away? 编辑:注释掉Toast会导致错误消失吗? If so, this is likely the problem. 如果是这样,这可能是问题所在。 Work your way through the Android tutorials . Android教程中进行工作

How about if you remove 如果删除该怎么办

android:onClick="SaveRegistration"

from your layout and do it in your code, eg 从您的布局并在您的代码中进行操作,例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM