问题
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)
回答1:
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.
回答2:
Substitude the following line:
whatToUploadDialog.setContentView(R.layout.submitscoreprompt);
with:
whatToUploadDialog.setView(R.layout.submitscoreprompt);
回答3:
Try calling
.setTitle(R.string.uploadedScoreTitle);
before
.setContentView(R.layout.submitscoreprompt);
来源:https://stackoverflow.com/questions/7568479/error-requestfeature-must-be-called-before-adding-content-still-wont-work