gpt4 book ai didi

android - 错误 : requestFeature() must be called before adding content - Still won't work

转载 作者:太空宇宙 更新时间:2023-11-03 11:34:01 38 4
gpt4 key购买 nike

我知道过去有人问过类似的问题,但即使有这些建议,我似乎也根本无法解决这个问题。

我在“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)

最佳答案

我遇到了同样的问题。我发现只有在执行以下两项操作时才会出现问题:

  • 我不使用 Activity 管理对话框 (activity.showDialog() ->activity.onCreateDialog()/onPrepareDialog())

  • 我做dialog.findViewById()(这确实是行的区别在成功或 requestFeature 异常之间!)。

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

dialog.findViewById() 导致问题,因为它调用

dialog.getWindow().getDecorView()

getDecorView() 的方法 javadoc 说:

Note that calling this function for the first time "locks in" various window characteristics as described in {@link #setContentView(View, android.view.ViewGroup.LayoutParams)}.

这不是很好吗,findViewById() 有一个副作用,它会导致看似正确的应用程序崩溃。为什么 Activity 托管对话框和普通对话框之间存在差异我不知道,但我猜 getDecorView()Activity 托管对话框有一些魔力.

我之所以这样做是因为我从使用 Activity 管理的对话框转变为自己处理对话框。

我的解决方案是使用 rootView.findViewById() 来操作 rootView,而不是操作对话框。

关于android - 错误 : requestFeature() must be called before adding content - Still won't work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7568479/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com