java.lang.illegalstateexception could not find a method (view) in the activity class android fragment

我是研究僧i 提交于 2019-11-27 09:03:20

You can't register the onClick callback(using android:onClick) for the Button(from the layout of the Dialog) in the Fragment class because Android will not find it as it will search only the Activity for a method matching that name(Btn_pumpInfo_Clicked) and it will throw that exception. Instead look for the Button in the Dialog and assign it a normal listener:

//...
dialog.setContentView(R.layout.pump_menu)
Button b = (Button) dialog.findViewById(R.id.Button_pumpInfo);
b.setOnClickListener(new OnClickListener() {

   @Override
   public void onCLick(View v) {
       // profit
   }
});

Or move the method :

public void Btn_pumpInfo_Clicked(View v) {
    // TODO Auto-generated method stub
    Toast.makeText(getActivity(), "You clicked on Item 1",
            Toast.LENGTH_LONG).show();
}

in the Activity class if it fits you.

If this bug is happening only on Android 2.x check your Activity class hierarchy for Android 4 specific class members:

public class HomeActivity extends Activity {

  private android.app.ActionBar.LayoutParams customTitleParams;

  ....
}

The class android.app.ActionBar.LayoutParams is not available on Android 2.x and this can cause the problem.

This can also happen when the method to call on onClick of the View(Button) isn't accessible or isn't available in the respective activity. Steps to check:

  • Check the method name in the value for android:onClick attribute matches the method actually written.
  • Check the access modifier of the method is public so as to be available to the execute.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!