且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

java.lang.illegalstateexception在活动类android片段中找不到方法(视图)

更新时间:2023-09-21 08:56:58

您不能为 注册 onClick 回调(使用 android:onClick)Fragment 类中的按钮(来自 Dialog 的布局),因为 Android 不会找到它,因为它会搜索Activity 用于匹配该名称的方法(Btn_pumpInfo_Clicked),它将抛出该异常.而是在 Dialog 中寻找 Button 并为其分配一个普通的侦听器:

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
   }
});

或者移动方法:

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

Activity 类中,如果它适合你.

in the Activity class if it fits you.