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 when a button is clicked in the 1st activity. But it is giving error. And I have imported correct android.view.View
package.
The same question was asked by someone else earlier, but I didn't get the solution, here is the error-prone code.
activity_main.xml:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="22dp"
android:layout_toRightOf="@+id/textView1"
android:onClick="startActivityB"
android:clickable="true"
android:text="startb" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/button1"
android:layout_marginTop="31dp"
android:onClick="finishA"
android:clickable="true"
android:text="FinishA" />
<Button
MainActivity.java: (and this is the 1st activity)
protected void startActivityB(View v) {
Intent intent = new Intent(getApplicationContext(), Activity_B.class);
startActivity(intent);
}
protected void finishA(View v)
{
MainActivity.this.finish();
}
This are the errors I got in logcat:
D/Avtivity_A(1333): onStart()of Activity_A started
D/Avtivity_A(1333): onResume()Activity_A started
D/AndroidRuntime(1333): Shutting down VM
W/dalvikvm(1333): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
E/AndroidRuntime(1333): FATAL EXCEPTION: main
E/AndroidRuntime(1333): 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'
E/AndroidRuntime(1333): at android.view.View$1.onClick(View.java:2059)
E/AndroidRuntime(1333): at android.view.View.performClick(View.java:2408)
E/AndroidRuntime(1333): at android.view.View$PerformClick.run(View.java:8816)
E/AndroidRuntime(1333): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime(1333): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(1333): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(1333): at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(1333): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(1333): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(1333): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(1333): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(1333): at dalvik.system.NativeStart.main(Native Method)
In your code you have called a function somewhere (Not in the code you have provided in your question)
It says that in your mainActivity the function which is implemented on
button2
click not thebutton1
which you shown in your code. it may Contains a line which says a call tofinishA(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 thefinishA()
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-A
andActivity-B
classes are in same package , by creating an object of classActivity-A
inActivity-B
class (note: if you are going to use class name without creating an object you will have to useprotected static
while declaring the function.)With
Call the method in Activity-B class as(it will work only If the both classes are in same package)
With
Call the method in Activity-B class as
With
Call the method in Activity-B class as
with
Call the method in Activity-B class as(it will work only If the both classes are in same package)
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 :
and in the class you must implement a function:
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 bepublic
as in the sample I showed above. When I test this in my app I get the same error when I make it protected.