Mohammad Ali Nematollahi Mohammad Ali Nematollahi -5 years ago 1260
Android Question

IllegalStateException: Could not find method insert(View) in a parent or ancestor Context class

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 with id 'button' at$DeclaredOnClickListener.resolveMethod( at$DeclaredOnClickListener.onClick(
at android.view.View.performClick(
at android.view.View$
at android.os.Handler.handleCallback(
at android.os.Handler.dispatchMessage(
at android.os.Looper.loop(
at java.lang.reflect.Method.invoke(Native Method)

Answer Source

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

So, that is the issue:

"Insert Button" is being used in However, that class does not have any method public void insert(View view)

So, add this to your

public void insert(View view){
    // Add the code that you want
    // Or do nothing if you want


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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download