android DialogFragment android:onClick =“buttonCancel”导致IllegalStateException无法找到方法

我的Dialog Fragment有问题。 我想使用android:onClick属性,因为在我看来代码更清晰。

在我的布局中,我有以下声明:

现在我的DialogFragment

 import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class DialogNewDatabase extends DialogFragment { public DialogNewDatabase() { // Empty constructor required for DialogFragment super(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView (inflater, container, savedInstanceState); View view = inflater.inflate(R.layout.dialog_new_database, container); getDialog().setTitle("Hello"); return view; } @Override public void onCreate(Bundle bundle) { setCancelable(true); setRetainInstance(true); super.onCreate(bundle); } @Override public void onDestroyView() { if (getDialog() != null && getRetainInstance()) getDialog().setDismissMessage(null); super.onDestroyView(); } public void buttonCancel (View view) { dismiss(); } public void buttonOK (View view) { } } 

我现在当我尝试点击取消按钮时,我得到:

 java.lang.IllegalStateException: Could not find a method buttonCancel(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'dialog_new_database_button_cancel' at android.view.View$1.onClick(View.java:3031) at android.view.View.performClick(View.java:3511) at android.view.View$PerformClick.run(View.java:14105) at android.os.Handler.handleCallback(Handler.java:605) at android.os.Handler.dispatchMessage(Handler.java:92) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4482) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NoSuchMethodException: buttonCancel [class android.view.View] at java.lang.Class.getConstructorOrMethod(Class.java:460) at java.lang.Class.getMethod(Class.java:915) at android.view.View$1.onClick(View.java:3024) ... 11 more 

任何想法? 这可能与我使用import android.support.v4.app.DialogFragment(支持v4)的事实有某种关联吗? 如何解决(我仍然更喜欢在xml布局中使用android:onClick)。

我会尝试一种不同的方法,对我来说很好:

  1. 在片段中实现OnClickListener:

     public class DialogNewDatabase extends DialogFragment implements OnClickListener` 
  2. 在xml中有一个具有唯一ID的按钮,它不需要android:clickable

      
  3. 覆盖片段中的onClick()方法并在您的点击上插入反应:

     public void onClick(View v) { switch (v.getId()) { case R.id.dialog_new_database_button_cancel: // your stuff here this.dismiss(); break; default: break; } } 
  4. import必要的:

     import android.view.View.OnClickListener; 
  5. 启动按钮上的onClickListener:

     private Button bCancel = null; bCancel = (Button) findViewById(R.id.dialog_new_database_button_cancel); bCancel.setOnClickListener(this); // it is possible that you might need the refrence to the view. // replace 2nd line with (Button) getView().findViewById(...); 

    这样,您可以在同一个onClick方法中处理更多可点击的按钮。 您只需要使用可点击小部件的正确ID添加更多案例。

我不认为这与支持片段有关。

这个问题似乎是因为你正在注册一个onClick on XML,它基于点击时片段被绑定的活动而激活。

由于您的“buttonCancel”方法在活动中不存在(因为它在片段内),因此失败。

我认为这不是一个理想的解决方案,但你可以在你的活动上注册你的“buttonCancel”方法以消除该错误,并使在活动上注册的“buttonCancel”方法只调用存在于该活动中的方法。片段,以防您希望在片段中保留您的操作/视图行为。

尝试:

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView (inflater, container, savedInstanceState); View view = inflater.inflate(R.layout.dialog_new_database, container); getDialog().setTitle("Hello"); return view; private void buttonCancel (View view) { dismiss(); } private void buttonOK (View view) { } }