错误:requestFeature()必须在添加内容之前,被称为 - 仍然无法工作 [英] error: requestFeature() must be called before adding content - Still won't work
问题描述
我知道,类似的问题已被要求在过去,但我似乎无法得到这个工作在所有甚至与建议。
我得到了上述异常结束显示()命令。
公共无效的onCreate(包savedInstanceState){
尝试{
super.onCreate(savedInstanceState);
的setContentView(R.layout.submitscoredummylayout);
scoreloopInit();
AlertDialog whatToUploadDialog;
whatToUploadDialog =新AlertDialog.Builder(YanivSubmitScoreActivity.this).create();
whatToUploadDialog.setContentView(R.layout.submitscoreprompt);
whatToUploadDialog.setTitle(R.string.uploadedScoreTitle);
whatToUploadDialog.setCancelable(假);
((复选框)whatToUploadDialog.findViewById(R.id.ckbScoreloop))setChecked(settings.getUploadToSL());
((复选框)whatToUploadDialog.findViewById(R.id.ckbFacebook))setChecked(settings.getUploadToFB());
((复选框)whatToUploadDialog.findViewById(R.id.ckbScoreloop))。setOnCheckedChangeListener(新OnCheckedChangeListener(){
@覆盖
公共无效onCheckedChanged(CompoundButton ckBox,布尔器isChecked){
settings.setUploadToSL(器isChecked,真正的);
findViewById(R.id.btnYes).setEnabled(器isChecked || settings.getUploadToFB());
}
});
((复选框)whatToUploadDialog.findViewById(R.id.ckbFacebook))。setOnCheckedChangeListener(新OnCheckedChangeListener(){
@覆盖
公共无效onCheckedChanged(CompoundButton ckBox,布尔器isChecked){
settings.setUploadToFB(器isChecked,真正的);
findViewById(R.id.btnYes).setEnabled(器isChecked || settings.getUploadToSL());
}
});
whatToUploadDialog.findViewById(R.id.btnYes).setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
submitScore(SUBMIT_UPLOAD_TO_SL);
whatToUploadDialog.dismiss();
}
});
whatToUploadDialog.findViewById(R.id.btnNo).setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
whatToUploadDialog.dismiss();
完();
}
});
whatToUploadDialog.show();
}
logcat的:
W / System.err的(14969):android.util.AndroidRuntimeException:requestFeature()必须添加内容之前调用
W / System.err的(14969):在com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:184)
W / System.err的(14969):在com.android.internal.app.AlertController.installContent(AlertController.java:198)
W / System.err的(14969):在android.app.AlertDialog.onCreate(AlertDialog.java:251)
W / System.err的(14969):在android.app.Dialog.dispatchOnCreate(Dialog.java:307)
W / System.err的(14969):在android.app.Dialog.show(Dialog.java:225)
W / System.err的(14969):在ui.YanivSubmitScoreActivity.onCreate(YanivSubmitScoreActivity.java:105)
W / System.err的(14969):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
W / System.err的(14969):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
W / System.err的(14969):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
W / System.err的(14969):在android.app.ActivityThread.access $ 2300(ActivityThread.java:125)
W / System.err的(14969):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2033)
W / System.err的(14969):在android.os.Handler.dispatchMessage(Handler.java:99)
W / System.err的(14969):在android.os.Looper.loop(Looper.java:123)
W / System.err的(14969):在android.app.ActivityThread.main(ActivityThread.java:4627)
W / System.err的(14969):在java.lang.reflect.Method.invokeNative(本机方法)
W / System.err的(14969):在java.lang.reflect.Method.invoke(Method.java:521)
W / System.err的(14969):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:871)
W / System.err的(14969):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
W / System.err的(14969):在dalvik.system.NativeStart.main(本机方法)
我经历了同样的问题。我发现,只要我两者都做以下事情发生问题:
-
我不使用活动管理对话框(
activity.showDialog()
- >activity.onCreateDialog()
/在prepareDialog()
) -
我
dialog.findViewById()
(这确实是一线之差 成功或requestFeature异常的!)。
最终生成器dialogBuilder =新AlertDialog.Builder(活动);
b.setView(rootView);
b.setIcon(android.R.drawable.ic_dialog_info);
b.setTitle(R.string.message_of_the_day_title);
b.setCancelable(假);
对话框= b.createDialog();
dialog.findViewById(R.id.myid); //这是问题
在 dialog.findViewById()
将导致问题,因为它要求
dialog.getWindow()。getDecorView()
和的方法的javadoc getDecorView()
说:
请注意,各种调用此函数首次锁定 作为{@link #setContentView(查看描述窗口的特点, android.view.ViewGroup.LayoutParams)}。
是不是很好, findViewById()
有副作用,导致看似正确的应用程序崩溃。为什么有活动
管理对话和正常的对话,我不知道之间的差别,但我猜 getDecorView()
做一些魔术活动
管理对话框。
我做了上面的,因为我使用活动
管理对话框来处理对话框自己移动。
我的解决方案是操纵rootView,使用 rootView.findViewById()
,而不是操纵对话框。
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)
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 difference between success or the requestFeature exception!).
final Builder dialogBuilder = new AlertDialog.Builder(activity);
b.setView(rootView);
b.setIcon(android.R.drawable.ic_dialog_info);
b.setTitle(R.string.message_of_the_day_title);
b.setCancelable(false);
dialog = b.createDialog();
dialog.findViewById(R.id.myid); // this is the problem
The dialog.findViewById()
causes the problem because it calls
dialog.getWindow().getDecorView()
and the method javadoc of getDecorView()
says:
Note that calling this function for the first time "locks in" various window characteristics as described in {@link #setContentView(View, android.view.ViewGroup.LayoutParams)}.
Isn't that nice, findViewById()
has a side effect which causes seemingly correct applications to crash. Why there's a difference between Activity
managed dialogs and normal dialogs I do not know, but I guess getDecorView()
does some magic for Activity
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.
这篇关于错误:requestFeature()必须在添加内容之前,被称为 - 仍然无法工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!