[英]android DialogFragment android:onClick=“buttonCancel” causes IllegalStateException could not find a method
我的对话框片段有问题。 我想使用android:onClick属性,因为在我看来代码更清楚了。
在我的布局中,我有以下声明:
<Button
android:id="@+id/dialog_new_database_button_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/button_cancel"
android:maxLines="1"
style="?android:attr/buttonBarButtonStyle"
android:onClick="buttonCancel"
/>
现在我的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(support v4)的事实有关吗? 如何解决这个问题(我仍然希望在xml布局中使用android:onClick)。
我会尝试另一种对我有用的方法:
在片段中实现OnClickListener:
public class DialogNewDatabase extends DialogFragment implements OnClickListener`
有在XML的唯一的ID,这并不需要一个按钮, android:clickable
<Button android:id="@+id/dialog_new_database_button_cancel" />
覆盖片段中的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; } }
导入必要的:
import android.view.View.OnClickListener;
在按钮上启动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而产生的,该onClick基于片段在单击时被绑定的活动而触发。
由于您的“ 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) {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.