简体   繁体   中英

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

Logcat errorlog

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); it change the layout to another xml file newregistration.xml

Later on I created a Addregistration.java file where I placed the event handler. The event handler needed to be placed in my main java file and now it works.

What is the difference between: 1. setContentView(R.layout.newregistration); 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.

Does your class extend Activity like below?

From Android site:

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? If so, this is likely the problem. Work your way through the Android tutorials .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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