错误:在添加内容之前必须调用requestFeature() - 仍然无法正常工作

the*_*itz 3 android android-alertdialog

我知道过去曾经问过类似的问题,但即使有了这些建议,我似乎也无法做到这一点.

我在"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();
}
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

arb*_*erg 11

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

  • 我不使用活动管理对话框(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
Run Code Online (Sandbox Code Playgroud)

dialog.findViewById()问题,因为它要求的原因

dialog.getWindow().getDecorView()
Run Code Online (Sandbox Code Playgroud)

和方法javadoc getDecorView()说:

请注意,第一次调用此函数会"锁定"各种窗口特征,如{@link #setContentView(View,android.view.ViewGroup.LayoutParams)}中所述.

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

我之所以这样做是因为我Activity自己使用托管对话框来处理对话框.

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

  • 究竟!我认为这是发生这种情况的主要原因!在通过无意义的解决方案查看有关此问题的问题之后,它将最终运作!想要添加一件事:如果一个人调用`dialog.show()`BEFORE` dialog.findViewById`一切正常.我认为这与在第一次调用`dialog.show()之前没有充气的DecorView有关. (2认同)