Create button quit using onClick

0

I have a problem with a button on android. I need to create an 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"/>

This is the error:

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)
    
asked by anonymous 24.10.2016 / 00:30

2 answers

0

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

Example in the 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();
     }
});
    
24.10.2016 / 15:43
0

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

    
24.10.2016 / 04:00