错误:在添加内容之前必须调用 requestFeature() - 仍然不起作用
我知道过去也曾提出过类似的问题,但即使有这些建议,我似乎也根本无法解决这个问题。
我在“show()”命令上遇到上述异常终止。
public void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.submitscoredummylayout);
scoreloopInit();
AlertDialog whatToUploadDialog;
whatToUploadDialog = new AlertDialog.Builder(YanivSubmitScoreActivity.this).create();
whatToUploadDialog.setContentView(R.layout.submitscoreprompt);
whatToUploadDialog.setTitle(R.string.uploadedScoreTitle);
whatToUploadDialog.setCancelable(false);
((CheckBox)whatToUploadDialog.findViewById(R.id.ckbScoreloop)).setChecked(settings.getUploadToSL());
((CheckBox)whatToUploadDialog.findViewById(R.id.ckbFacebook)).setChecked(settings.getUploadToFB());
((CheckBox) whatToUploadDialog.findViewById(R.id.ckbScoreloop)).setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton ckBox, boolean isChecked) {
settings.setUploadToSL(isChecked,true);
findViewById(R.id.btnYes).setEnabled(isChecked||settings.getUploadToFB());
}
});
((CheckBox) whatToUploadDialog.findViewById(R.id.ckbFacebook)).setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton ckBox, boolean isChecked) {
settings.setUploadToFB(isChecked,true);
findViewById(R.id.btnYes).setEnabled(isChecked||settings.getUploadToSL());
}
});
whatToUploadDialog.findViewById(R.id.btnYes).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
submitScore(SUBMIT_UPLOAD_TO_SL);
whatToUploadDialog.dismiss();
}
});
whatToUploadDialog.findViewById(R.id.btnNo).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
whatToUploadDialog.dismiss();
finish();
}
});
whatToUploadDialog.show();
}
日志猫:
W/System.err(14969): android.util.AndroidRuntimeException: requestFeature() must be called before adding content
W/System.err(14969): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:184)
W/System.err(14969): at com.android.internal.app.AlertController.installContent(AlertController.java:198)
W/System.err(14969): at android.app.AlertDialog.onCreate(AlertDialog.java:251)
W/System.err(14969): at android.app.Dialog.dispatchOnCreate(Dialog.java:307)
W/System.err(14969): at android.app.Dialog.show(Dialog.java:225)
W/System.err(14969): at ui.YanivSubmitScoreActivity.onCreate(YanivSubmitScoreActivity.java:105)
W/System.err(14969): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
W/System.err(14969): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
W/System.err(14969): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
W/System.err(14969): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
W/System.err(14969): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
W/System.err(14969): at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err(14969): at android.os.Looper.loop(Looper.java:123)
W/System.err(14969): at android.app.ActivityThread.main(ActivityThread.java:4627)
W/System.err(14969): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(14969): at java.lang.reflect.Method.invoke(Method.java:521)
W/System.err(14969): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
W/System.err(14969): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
W/System.err(14969): at dalvik.system.NativeStart.main(Native Method)
I know that similar questions have been asked in the past but I can't seem to get this working at all even with the suggestions.
I get the above abend on the "show()" command.
public void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.submitscoredummylayout);
scoreloopInit();
AlertDialog whatToUploadDialog;
whatToUploadDialog = new AlertDialog.Builder(YanivSubmitScoreActivity.this).create();
whatToUploadDialog.setContentView(R.layout.submitscoreprompt);
whatToUploadDialog.setTitle(R.string.uploadedScoreTitle);
whatToUploadDialog.setCancelable(false);
((CheckBox)whatToUploadDialog.findViewById(R.id.ckbScoreloop)).setChecked(settings.getUploadToSL());
((CheckBox)whatToUploadDialog.findViewById(R.id.ckbFacebook)).setChecked(settings.getUploadToFB());
((CheckBox) whatToUploadDialog.findViewById(R.id.ckbScoreloop)).setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton ckBox, boolean isChecked) {
settings.setUploadToSL(isChecked,true);
findViewById(R.id.btnYes).setEnabled(isChecked||settings.getUploadToFB());
}
});
((CheckBox) whatToUploadDialog.findViewById(R.id.ckbFacebook)).setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton ckBox, boolean isChecked) {
settings.setUploadToFB(isChecked,true);
findViewById(R.id.btnYes).setEnabled(isChecked||settings.getUploadToSL());
}
});
whatToUploadDialog.findViewById(R.id.btnYes).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
submitScore(SUBMIT_UPLOAD_TO_SL);
whatToUploadDialog.dismiss();
}
});
whatToUploadDialog.findViewById(R.id.btnNo).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
whatToUploadDialog.dismiss();
finish();
}
});
whatToUploadDialog.show();
}
Logcat:
W/System.err(14969): android.util.AndroidRuntimeException: requestFeature() must be called before adding content
W/System.err(14969): at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:184)
W/System.err(14969): at com.android.internal.app.AlertController.installContent(AlertController.java:198)
W/System.err(14969): at android.app.AlertDialog.onCreate(AlertDialog.java:251)
W/System.err(14969): at android.app.Dialog.dispatchOnCreate(Dialog.java:307)
W/System.err(14969): at android.app.Dialog.show(Dialog.java:225)
W/System.err(14969): at ui.YanivSubmitScoreActivity.onCreate(YanivSubmitScoreActivity.java:105)
W/System.err(14969): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
W/System.err(14969): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
W/System.err(14969): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
W/System.err(14969): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
W/System.err(14969): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
W/System.err(14969): at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err(14969): at android.os.Looper.loop(Looper.java:123)
W/System.err(14969): at android.app.ActivityThread.main(ActivityThread.java:4627)
W/System.err(14969): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(14969): at java.lang.reflect.Method.invoke(Method.java:521)
W/System.err(14969): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
W/System.err(14969): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
W/System.err(14969): at dalvik.system.NativeStart.main(Native Method)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我遇到了同样的问题。我发现只有当我执行以下两项操作时才会出现该问题:
我不使用活动管理对话框 (
activity.showDialog()
->activity.onCreateDialog()
/onPrepareDialog()
)我做
dialog.findViewById()
(这确实是行差异成功或 requestFeature 异常之间!)。
dialog.findViewById()
会导致问题,因为它调用了getDecorView()
的方法 javadoc 说:这不是很好吗,
findViewById()
有一个副作用,会导致看似正确的应用程序崩溃。我不知道为什么Activity
托管对话框和普通对话框之间存在差异,但我猜getDecorView()
对Activity
托管对话框有一些魔力。我之所以执行上述操作,是因为我从使用
Activity
托管对话框转向自己处理对话框。对我来说,解决方案是使用 rootView.findViewById() 来操作 rootView,而不是操作对话框。
I experienced the same problem. I found that the problem only occurs if I do both of the following things:
I don't use activity managed dialogs (
activity.showDialog()
->activity.onCreateDialog()
/onPrepareDialog()
)I do
dialog.findViewById()
(and this is indeed the line differencebetween success or the requestFeature exception!).
The
dialog.findViewById()
causes the problem because it callsand the method javadoc of
getDecorView()
says:Isn't that nice,
findViewById()
has a side effect which causes seemingly correct applications to crash. Why there's a difference betweenActivity
managed dialogs and normal dialogs I do not know, but I guessgetDecorView()
does some magic forActivity
managed dialogs.I did the above because I moved from using
Activity
managed dialogs to handling dialogs myself.The solution for me is to manipulate the rootView, using
rootView.findViewById()
, instead of manipulating the dialog.将以下行: 替换
为:
Substitude the following line:
with:
尝试
先打电话
Try calling
before