且构网

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

java.lang.illegalstateexception找不到在活动课的Andr​​oid片段的方法(视图)

更新时间:2023-11-16 11:53:10

您不能注册的onClick 回调(使用安卓的onClick )的按钮(从布局的对话框)的片段类,因为Android将找不到它,因为它会搜索活动的方法,该名称匹配( Btn_pumpInfo_Clicked ),它会抛出该异常。相反,查找按钮对话框并为它指定一个正常的监听器:

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.