Haren Sarma
Haren Sarma

Reputation: 2553

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.

Upvotes: 1

Views: 3569

Answers (2)

Rohit5k2
Rohit5k2

Reputation: 18112

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

Upvotes: 5

Renan Ferreira
Renan Ferreira

Reputation: 2150

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.

Upvotes: 0

Related Questions