简体   繁体   中英

Method doesn't exist (MainActivity.java)

I am very new to Android. Please give me a solution. I am in confusion. I have below line of code in

activity_main.xml file:

<ImageView
        android:id="@+id/exit_img"
        android:src = "@drawable/exit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:layout_alignParentEnd="true"
        android:onClick="exitsys()"
        android:resizeMode="horizontal|vertical" />

and in MainActivity.java file below lines of code:

public void exitsys(View v){

                    System.exit(0);

    }

But Android Studio is giving error that "Method exitsys() is missing in 'MainActivity' or has incorrect signature"

I don't know what mistake I did. Also as information, I am new to Java as well.

Remove "()" from method name

<ImageView
    android:id="@+id/exit_img"
    android:src = "@drawable/exit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:adjustViewBounds="true"
    android:layout_alignParentEnd="true"
    android:onClick="exitsys"
    android:resizeMode="horizontal|vertical" />

Also to exit an activity use finish() ; using System.exit(0) is a bad idea.

So use this

public void exitsys(View v){
                finish();
}

If you want to exit from the Activity, you should use the method

finish();

It will close the Activity. If your app don't have any other activity bellow, it will close the application. You seem to be very new to the Android universe. Take a look at the trainning guide in the developer website.

http://developer.android.com/training/index.html

It is very instructive.

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