Create quit button using onClick

Asked

Viewed 879 times

0

I’m having a problem with a button on android. I need to create a exit button, but when the button is selected an error appears.

This is the Dashboard class:

public class Dashboard extends Activity {

@Override
protected void onCreate(Bundle saveInstanceState) {

   super.onCreate(saveInstanceState);
   setContentView(R.layout.layout_dashboard);

}

public void sairApp(View view){
    this.finish();
}

}

This is the button:

<Button

      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="50dp"
      android:layout_marginTop="80dp"
      android:clickable="true"
      android:drawableTop="@drawable/sair"
      android:onClick="sairApp"
      android:text="@string/sair"
      android:textColor="#062f3c"
      android:textStyle="bold"/>

That is the mistake:

java.lang.IllegalStateException: Could not find method sairApp(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.widget.Button
                                                                    at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:4506)
                                                                    at android.view.View$DeclaredOnClickListener.onClick(View.java:4470)
                                                                    at android.view.View.performClick(View.java:5225)
                                                                    at android.view.View$PerformClick.run(View.java:21195)
                                                                    at android.os.Handler.handleCallback(Handler.java:739)
                                                                    at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                    at android.os.Looper.loop(Looper.java:148)
                                                                    at android.app.ActivityThread.main(ActivityThread.java:5451)
                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

2 answers

0

Have you set any theme in your XML? If you have set then the problem might be this. Look if this answer helps you: onClick does not work Anyway, I do not recommend defining the methods in XML, this makes it difficult to reuse XML. Although more laborious, it is always better to set the click via same code.

0


It is easier and better in the usability of the application that you assign an ID to the button and use the Onclicklistener to identify the click, if not you will need to create a method for each Button of your layout.

Example in XML declaration:

<Button
    android:id="@+id/finish_app"
    ... />

Java example:

Button finish_app = (Button) findViewById(R.id.finish_app);
finish_app.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
        this.finish();
     }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.