android - 对话问题 : requestFeature() must be called before adding content

标签 android dialog

我正在创建一个包含 EditText 的自定义对话框,以便我可以从用户那里获取文本数据:

final EditText newKey = (EditText) findViewById(R.id.dialog_result);
AlertDialog.Builder keyBuilder = new AlertDialog.Builder(StegDroid.this);
keyBuilder
.setCancelable(false)
.setPositiveButton("Try Again", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        Log.v("Dialog","New Key: "+newKey.getText().toString());
    }
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
       }
   });
AlertDialog dialog = keyBuilder.create();
dialog.setTitle("Decryption Failed");
dialog.setContentView(R.layout.decrypt_failed_dialog);
dialog.show();

但是我总是遇到这个异常:

01-11 18:49:00.507: ERROR/AndroidRuntime(3461): android.util.AndroidRuntimeException: requestFeature() must be called before adding content
01-11 18:49:00.507: ERROR/AndroidRuntime(3461):     at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:181)
01-11 18:49:00.507: ERROR/AndroidRuntime(3461):     at com.android.internal.app.AlertController.installContent(AlertController.java:199)
01-11 18:49:00.507: ERROR/AndroidRuntime(3461):     at android.app.AlertDialog.onCreate(AlertDialog.java:251)

...

dialog.show() 行。我应该怎么做才能摆脱这种情况?

最佳答案

您需要在创建对话框之前设置自定义 View 。如果您使用 AlertDialog 为您提供的默认正面和负面按钮,您还需要使用 setView(View) 而不是 setContentView() >.

final EditText newKey = (EditText) findViewById(R.id.dialog_result);
AlertDialog.Builder keyBuilder = new AlertDialog.Builder(StegDroid.this);
keyBuilder
.setCancelable(false)
.setPositiveButton("Try Again", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        Log.v("Dialog","New Key: "+newKey.getText().toString());
    }
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
       }
   });
keyBuilder.setTitle("Decryption Failed");
keyBuilder.setView(getLayoutInflater().inflate(R.layout.decrypt_failed_dialog, null));
AlertDialog dialog = keyBuilder.create();
dialog.show();

关于android - 对话问题 : requestFeature() must be called before adding content,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4661459/

相关文章:

android - 通知中的 Intent 选择器

java - 如何访问下载接收器中的本地文件名?

android - "You need to use a theme.appcompat theme"以Theme.AppCompat为主题,Sdk v23

android - 如何在 Android 增强现实中检测物理对象?

对话框内的 gwt tabindex

android - 如何用 Luaj 评估一个简单的表达式?

javascript - 如何更改 jQuery UI 对话框的背景颜色?

javascript - 在XPage上添加JQuery对话框的方法

java - eclipse 中的 MessageDialog 不工作

angular - 事件发射器工作一次但不是第二次 Angular 8