0

这个我完全不明白,我正在尝试显示一个没有这样标题的自定义对话框

d.setContentView(R.layout.incident_dialog_layout);
d.requestWindowFeature(Window.FEATURE_NO_TITLE);

这样做给了我一个错误

07-25 11:56:23.737: E/AndroidRuntime(15680): android.util.AndroidRuntimeException: requestFeature() must be called before adding content
07-25 11:56:23.737: E/AndroidRuntime(15680):    at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:215)
07-25 11:56:23.737: E/AndroidRuntime(15680):    at android.app.Dialog.requestWindowFeature(Dialog.java:1031)
07-25 11:56:23.737: E/AndroidRuntime(15680):    at ecm2.android.MainActivity.createNewDialog(MainActivity.java:1121)
07-25 11:56:23.737: E/AndroidRuntime(15680):    at ecm2.android.MainActivity$ListViews.onListItemClick(MainActivity.java:614)
07-25 11:56:23.737: E/AndroidRuntime(15680):    at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
07-25 11:56:23.737: E/AndroidRuntime(15680):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
07-25 11:56:23.737: E/AndroidRuntime(15680):    at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
07-25 11:56:23.737: E/AndroidRuntime(15680):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2855)
07-25 11:56:23.737: E/AndroidRuntime(15680):    at android.widget.AbsListView$1.run(AbsListView.java:3529)
07-25 11:56:23.737: E/AndroidRuntime(15680):    at android.os.Handler.handleCallback(Handler.java:615)
07-25 11:56:23.737: E/AndroidRuntime(15680):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-25 11:56:23.737: E/AndroidRuntime(15680):    at android.os.Looper.loop(Looper.java:137)
07-25 11:56:23.737: E/AndroidRuntime(15680):    at android.app.ActivityThread.main(ActivityThread.java:4745)
07-25 11:56:23.737: E/AndroidRuntime(15680):    at java.lang.reflect.Method.invokeNative(Native Method)
07-25 11:56:23.737: E/AndroidRuntime(15680):    at java.lang.reflect.Method.invoke(Method.java:511)
07-25 11:56:23.737: E/AndroidRuntime(15680):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-25 11:56:23.737: E/AndroidRuntime(15680):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-25 11:56:23.737: E/AndroidRuntime(15680):    at dalvik.system.NativeStart.main(Native Method)

好的,很简单,只需更改请求的位置即可。所以我然后把它改成这个

d.requestWindowFeature(Window.FEATURE_NO_TITLE);
d.setContentView(R.layout.incident_dialog_layout);

对话框一直有效,直到我返回并单击创建对话框的项目时开始新活动时出现此错误

07-25 11:51:19.182: E/AndroidRuntime(15376): android.util.AndroidRuntimeException: requestFeature() must be called before adding content
07-25 11:51:19.182: E/AndroidRuntime(15376):    at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:215)
07-25 11:51:19.182: E/AndroidRuntime(15376):    at android.app.Dialog.requestWindowFeature(Dialog.java:1031)
07-25 11:51:19.182: E/AndroidRuntime(15376):    at ecm2.android.MainActivity.createNewDialog(MainActivity.java:1120)
07-25 11:51:19.182: E/AndroidRuntime(15376):    at ecm2.android.MainActivity$ListViews.onListItemClick(MainActivity.java:614)
07-25 11:51:19.182: E/AndroidRuntime(15376):    at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
07-25 11:51:19.182: E/AndroidRuntime(15376):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
07-25 11:51:19.182: E/AndroidRuntime(15376):    at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
07-25 11:51:19.182: E/AndroidRuntime(15376):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2855)
07-25 11:51:19.182: E/AndroidRuntime(15376):    at android.widget.AbsListView$1.run(AbsListView.java:3529)
07-25 11:51:19.182: E/AndroidRuntime(15376):    at android.os.Handler.handleCallback(Handler.java:615)
07-25 11:51:19.182: E/AndroidRuntime(15376):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-25 11:51:19.182: E/AndroidRuntime(15376):    at android.os.Looper.loop(Looper.java:137)
07-25 11:51:19.182: E/AndroidRuntime(15376):    at android.app.ActivityThread.main(ActivityThread.java:4745)
07-25 11:51:19.182: E/AndroidRuntime(15376):    at java.lang.reflect.Method.invokeNative(Native Method)
07-25 11:51:19.182: E/AndroidRuntime(15376):    at java.lang.reflect.Method.invoke(Method.java:511)
07-25 11:51:19.182: E/AndroidRuntime(15376):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-25 11:51:19.182: E/AndroidRuntime(15376):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-25 11:51:19.182: E/AndroidRuntime(15376):    at dalvik.system.NativeStart.main(Native Method)

那么它想让我做什么,因为我在设置内容后无法拥有它,也不能在我发送内容之前拥有它??????

4

2 回答 2

3

您收到该错误是因为您第二次设置对话框布局时(即当您从第二个活动返回时),您的屏幕中已经有了内容。

因此,第二次调用 requestWindowFeature 会导致错误。

您必须将该语句移至 onCreate() 方法,因此只调用一次 requestFeature,确保在调用之前不添加任何内容。

于 2012-07-25T16:17:10.653 回答
0

根据对该问题的第二条评论,我猜您是在 onStart 或 onResume 方法中调用 requestFeature() 。

requestFeature 和 setContentView 应该在 onCreate() 中调用。如果您在其他任何地方执行此操作,它将再次被调用并导致您的应用程序抛出错误。

于 2012-07-25T16:20:17.660 回答