- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这个把我逼到了精神错乱的边缘!
我有一个 Activity ,我在其中使用 onClickListener 初始化 Button View ,如下所示:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout);
mMyButton = (Button) findViewById(R.id.myButtonId);
mMyButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(ID_MYDIALOG);
}
});
}
我还编写了自己的对话框生成器类(单例),因为我的应用程序中还有其他几个 Activity,它们将使用一组通用的对话框,因此在 onCreateDialog(int id)
我调用的给定 Activity 的函数:
@Override
protected Dialog onCreateDialog(int id) {
Dialog dialog;
switch (id) {
case ID_MYDIALOG:
dialog = DialogBuilder.getInstance().getMyDialog(this, mUri, mListener);
break;
default:
dialog = null;
break;
}
return dialog;
}
mUri
和 mListener
变量在别处定义,我已经验证它们是有效的。单例 DialogBuilder
类的相关部分如下所示:
public AlertDialog getMyDialog(Context context, Uri uri, DialogInterface.OnClickListener listener) {
// Inflate the custom body for the AlertDialog.
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.myDialogBody, null);
// Get the data to show in the dialog.
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
cursor.moveToFirst();
// Create a suitable data adapter and use it to populate the dialog body with data.
MyDialogDataAdapter adapter = new MyDialogDataAdapter(context, cursor);
// Populate the GridView in the dialog body with data.
GridView grid = (GridView) view.findViewById(R.id.myDialogDataGrid);
grid.setAdapter(adapter);
return new AlertDialog.Builder(context)
.setIcon(R.drawable.myDialogIcon)
.setTitle(R.string.myDialogTitle)
.setView(view)
.setPositiveButton(R.string.myDialogDone, listener)
.create();
}
现在,当我运行我的应用程序并点击按钮(即 mMyButton
)时,我得到以下异常堆栈跟踪:
E/AndroidRuntime( 1247): FATAL EXCEPTION: main
E/AndroidRuntime( 1247): android.util.AndroidRuntimeException: requestFeature() must be called before adding content
E/AndroidRuntime( 1247): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:181)
E/AndroidRuntime( 1247): at com.android.internal.app.AlertController.installContent(AlertController.java:203)
E/AndroidRuntime( 1247): at android.app.AlertDialog.onCreate(AlertDialog.java:251)
E/AndroidRuntime( 1247): at android.app.Dialog.dispatchOnCreate(Dialog.java:307)
E/AndroidRuntime( 1247): at android.app.Activity.createDialog(Activity.java:886)
E/AndroidRuntime( 1247): at android.app.Activity.showDialog(Activity.java:2557)
E/AndroidRuntime( 1247): at android.app.Activity.showDialog(Activity.java:2524)
E/AndroidRuntime( 1247): at com.dbm.myApp.myActivity$4.onClick(myActivity.java:266)
E/AndroidRuntime( 1247): at android.view.View.performClick(View.java:2534)
E/AndroidRuntime( 1247): at android.view.View$PerformClick.run(View.java:9210)
E/AndroidRuntime( 1247): at android.os.Handler.handleCallback(Handler.java:587)
E/AndroidRuntime( 1247): at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime( 1247): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 1247): at android.app.ActivityThread.main(ActivityThread.java:3652)
E/AndroidRuntime( 1247): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 1247): at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 1247): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 1247): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 1247): at dalvik.system.NativeStart.main(Native Method)
W/ActivityManager( 212): Force finishing activity com.dbm.myApp/.myActivity
如果我正确地解释堆栈跟踪,问题就会在我点击按钮的那一刻触发(从 com.dbm.myApp.myActivity$4.onClick(myActivity.java:266)
开始,这是确切的 showDialog(ID_MYDIALOG);
行。
各位 Android 开发者怎么说?你认为是什么导致了我的问题?
可以公平地假设上述代码是有效的,根据编译和单元测试等。然而,给定的代码已被简化和剥离以适合论坛,因此,请不要努力评论命名约定等
最佳答案
我想我可能已经解决了这个问题:如果我出于某种原因创建了 AlertDialog:
return new AlertDialog.Builder(context)
.setIcon(R.drawable.myDialogIcon)
.setTitle(R.string.myDialogTitle)
.setView(view)
.setPositiveButton(R.string.myDialogDone, listener)
.show();
即使用 .show();
而不是 .create();
问题不再存在。我不知道为什么会这样,或者 AlertDialog.Builder::show()
函数与 AlertDialog.Builder::create();
函数有什么不同(超过明显的区别体现在各自的函数名称上)。
关于android - "android.util.AndroidRuntimeException: requestFeature() must be called before adding content"on showDialog(dialogId),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4826407/
为了创建对话框,我覆盖了以下方法: protected Dialog onCreateDialog(final int dialogId) { } 为了调用它,我正在使用: showDialog(id
我用一个对话框扩展了我的类(class),我在其中设置了内容 View 和其他按钮 Action 监听器等...发生的事情是,当我创建我的对话框时,它会正确显示背景。 但是一次又一次地打开它是内容 V
我现在使用 QB 有一段时间了,一切都运行良好。今天出现了一些问题,我无法再接收具有正确 dialogID 的 QBChatMessages。我发送消息: [self.dialog sendMessa
这个把我逼到了精神错乱的边缘! 我有一个 Activity ,我在其中使用 onClickListener 初始化 Button View ,如下所示: @Override protected voi
我是一名优秀的程序员,十分优秀!