I have launched the program in single project which work properly.
However, when I have copied and pasted into a bigger project, it gives me bellow error in logcat.
FATAL EXCEPTION: main Process: com.example.alan.mainactivity, PID: 11545 java.lang.IllegalStateException: Could not find method insert(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button' at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:307) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:266)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
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:5417)
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)
I found the error.
In layout second_layout.xml
, you defined Button
with android:onClick="insert"
This layout is included in activity_user_profile.xml
This activity_user_profile.xml
is used in Main2Activity.java
So, that is the issue:
"Insert Button" is being used in Main2Activity.java
. However, that class does not have any method public void insert(View view)
So, add this to your Main2Activity.java:
public void insert(View view){
// Add the code that you want
// Or do nothing if you want
}
Rememeber
If you set any onClick
event in a layout file (xml), you have to create the method in the parent activity that will use that layout.
I was facing this exact same issue. The issue was that I was calling a public method from the onClick in my xml layout. Inside that public method, I was calling another private method inside the same class.
I changed the class from private to public and it worked.
XML layout:
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="increment"
android:text="+" />
Class:
public void increment(View view){
quantity = quantity + 1;
display(quantity);
}
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + number);
}
I changed the display method from private to public and it worked.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With