4

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)
  • Could you please share activity_main.xml – guipivoto Feb 26 '16 at 02:04
  • Yes sure I think the problem is that I cannot read any text from second_layout.xml by Main3Activity.java – Mohammad Ali Nematollahi Feb 26 '16 at 02:14
  • I could not find yet. Basically, this error happens because you set onClick="insert" in one layout.xml.. Then, you are using this layout in some activity which does not have the method insert declared... In Main3Activity, this method is there... Not sure about other places – guipivoto Feb 26 '16 at 02:15

2 Answers2

10

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.

guipivoto
  • 18,327
  • 9
  • 60
  • 75
  • Dear @Guilherme I have added the bellow codes in Main2Activity. However it does not work – Mohammad Ali Nematollahi Feb 26 '16 at 02:28
  • public void insert(View view) and insertToDatabase(name,add); are inserted to Main2Activity.java – Mohammad Ali Nematollahi Feb 26 '16 at 02:29
  • Do you use second_layout.xml or activity_user_profile.xml in any other activity? – guipivoto Feb 26 '16 at 02:30
  • no just activity.xml is using in MainActivity.java. Main2Activity is using activity_user_profile.xml. and Main3Activity is using second_layout.xml – Mohammad Ali Nematollahi Feb 26 '16 at 02:34
  • Just for test, create insert method in all activities... Which Activity is opened when the error happens? – guipivoto Feb 26 '16 at 02:39
  • do you mean insert and insertToDatabase all code in all activities? – Mohammad Ali Nematollahi Feb 26 '16 at 02:40
  • Just "public void insert (View v) {return;}"... Android is searching that method in one activities.. we need to find which one. – guipivoto Feb 26 '16 at 02:42
  • when I have added to MainActivity.java it gives me errors Error:(55, 23) error: cannot find symbol variable editTextName Error:(56, 22) error: cannot find symbol variable editTextAdd Error:(69, 31) error: cannot find symbol variable editTextName Error:(70, 30) error: cannot find symbol variable editTextAdd – Mohammad Ali Nematollahi Feb 26 '16 at 02:46
  • Add just "public void insert (View v) {return;}" – guipivoto Feb 26 '16 at 02:47
  • I have added but no error no insert. It does not do anything – Mohammad Ali Nematollahi Feb 26 '16 at 02:49
  • Ok. This mean that the issue is in MainAcitivity.java. You have to add the method to this class... Now, you have to port whole method to this class since Android will search for insert on MainAcitivity.java – guipivoto Feb 26 '16 at 02:52
  • In fact, I don't use onClick from layout files... i like to create the listeners like this question: http://stackoverflow.com/a/3320148/4860513 – guipivoto Feb 26 '16 at 02:54
  • @W0rmH0le In my case I'm using Fragment and there I need to handle click event instead to receive in parent activity. Any suggestion !! – CoDe Nov 15 '17 at 00:58
  • 1
    @CoDe If you set android:onclick property in the XML, you must implement the method in parent activity.. If you want to implement the method inside Fragment, you should set a click listener (setOnClickListener(new View.OnClickListener()) instead of set via XML. – guipivoto Nov 16 '17 at 10:28
  • [**Data binding**](https://developer.android.com/topic/libraries/data-binding/index.html#method_references) integration better solution I found. – CoDe Nov 16 '17 at 10:36
1

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.

brass monkey
  • 5,841
  • 10
  • 36
  • 61
rakesh kumar
  • 87
  • 1
  • 6