Skip to content Skip to sidebar Skip to footer

Java.lang.illegalstateexeption: Could Not Find A Method Finisha(view) In The Activity Class

I am new to Android. I have started doing with ActivityLifeCycle app. In this I have 3 activity classes. From first activity I want to go to second activity class using intents whe

Solution 1:

In your xml for the activity you defined an onClick handler, and this name is the function name of the handler. I think you didn't implement the function.

i.E.

in the XML you have :

android:onClick="finishA"

and in the class you must implement a function:

publicvoidfinishA(View view)
{
}

Not sure if this is the cause, but it looks like it. If not, then post the XML and the class (the relevant parts of it).

Update

In your question the error references button2 and your XML is about button1, so you should show us the correct files.

Update

Your onClick handler is protected but it needs to be public as in the sample I showed above. When I test this in my app I get the same error when I make it protected.

Solution 2:

In your code you have called a function somewhere (Not in the code you have provided in your question)

java.lang.IllegalStateException: Could not find a method finishA(View) in the activity class com.example.lifecycle.MainActivity for onClick handler on view class android.widget.Button with id 'button2'

It says that in your mainActivity the function which is implemented on button2 click not the button1 which you shown in your code. it may Contains a line which says a call to finishA(View v) and when the compiler search for the same it is missing in the declaration of the class.

After Update to question

as suggested by Devolus You have used protected access modifier for your function finishA() you should use public for the same one instead of protected.

If you use protected as an access modifier then it makes the function available only to the class and the subclasses of the same class. Because of which when you are calling the finishA() from your activity B it is not able to find out the finishA() function.

The Protected variables and methods allow the class itself to access them, classes inside of the same package to access them, and subclasses of that class to access them

Where as using public access modifier the function will be available to each class which implements an object of the class Activity-A

You can read more at : http://www.java-made-easy.com/java-access-modifiers.html

and this question on SO will be helpful

You can also go on to use protected access modifier in your function if your bothe Activity-Aand Activity-B classes are in same package , by creating an object of class Activity-A in Activity-B class (note: if you are going to use class name without creating an object you will have to use protected static while declaring the function.)

With

protectedvoidfinishA(View v){
    MainActivity.this.finish();
}

Call the method in Activity-B class as(it will work only If the both classes are in same package)

ActivityAobj=newActivityA ();
obj.finishA(View v);

With

publicvoidfinishA(View v){
        MainActivity.this.finish();
    }

Call the method in Activity-B class as

ActivityAobj=newActivityA ();
obj.finishA(View v);

With

publicstaticvoidfinishA(View v){
        MainActivity.this.finish();
    }

Call the method in Activity-B class as

ActivityA.finishA(View v);

with

protectedstaticvoidfinishA(View v){
        MainActivity.this.finish();
    }

Call the method in Activity-B class as(it will work only If the both classes are in same package)

ActivityA.finishA(View v);

Post a Comment for "Java.lang.illegalstateexeption: Could Not Find A Method Finisha(view) In The Activity Class"