" android.util.AndroidRuntimeException:requestFeature()添加内容&QUOT之前,必须先调用;上的ShowDialog(dialogId) [英] "android.util.AndroidRuntimeException: requestFeature() must be called before adding content" on showDialog(dialogId)

查看:89
本文介绍了" android.util.AndroidRuntimeException:requestFeature()添加内容&QUOT之前,必须先调用;上的ShowDialog(dialogId)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这一个是推动我疯狂的边缘!

This one is driving me to the brink of insanity!

我有一个活动,我初始化按钮查看与onClickListener,像这样:

I have an activity where I initialize a Button view with an onClickListener, like so:

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

我也写我自己的对话框生成器类(单身),因为我有几个其他的活动在我的应用程序,它将会使用一套通用对话框,因此在 onCreateDialog(INT ID)函数在给定的活动我打电话:

I have also written my own dialog builder class (a singleton) since I have several other Activity's in my application which will be using a common set of dialogs, hence in the onCreateDialog(int id) function for the given Activity I call:

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

穆里 mListener 变量定义在其他地方,我已经验证它们是有效的。单身的相关部分 DialogBu​​ilder 类看起来是这样的:

The mUri and mListener variables are defined elsewhere and I have verified that they are valid. The relevant parts of the singleton DialogBuilder class looks like this:

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 )我得到以下异常堆栈跟踪:

Now, when I run my application and tap the button (i.e. mMyButton) I get the following exception stack trace:

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

如果我跨preT堆栈跟踪正确的问题被触发的那一刻我点击我的按钮(如 com.dbm.myApp.myActivity $ 4.onClick的(myActivity.java:266 )这是确切的的ShowDialog(ID_MYDIALOG); 在线)

If I interpret the stack trace correctly the issue is triggered the very moment I tap my button (as of com.dbm.myApp.myActivity$4.onClick(myActivity.java:266) which is the exact showDialog(ID_MYDIALOG); line).

你是什么,各位Android开发者说什么?什么都你觉得导致我的问题?

这是公平的假设,上述code是有效的,因为每个编译和单元测试等给出的code,然而,简化和剥离,以适应论坛,因此,请不要把为了发表评论的命名约定等。

It's fair to assume that the above code is valid, as per compilation and unit tests etc. The given code is, however, simplified and stripped to fit the forum, hence, please don't put an effort to comment the naming conventions etc.

推荐答案

我想我可能已经解决了这个问题:如果我因为某种原因创建AlertDialog有:

I think I might have solved the issue: If I for some reason create the AlertDialog with:

return new AlertDialog.Builder(context)
    .setIcon(R.drawable.myDialogIcon)
    .setTitle(R.string.myDialogTitle)
    .setView(view)
    .setPositiveButton(R.string.myDialogDone, listener)
    .show();

即。与 .show(); ,而不是 .create(的); 问题不再是present。我不知道为什么这个工程还是什么不同于 AlertDialog.Builder ::显示()功能 AlertDialog.Builder ::创建() ; 函数(超过明显的区别体现在相应的功能名称)

I.e. with .show(); instead of .create(); The issue is no longer present. I don't know why this works or what differs the AlertDialog.Builder::show() function from the AlertDialog.Builder::create(); function (more than the obvious difference reflected in respective function names).

这篇关于" android.util.AndroidRuntimeException:requestFeature()添加内容&QUOT之前,必须先调用;上的ShowDialog(dialogId)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆