简体   繁体   English

Android在弹出活动上显示AlertDialog

[英]Android show AlertDialog on popup Activity

I am trying to add an AlertDialog to popup from a popup Activity (which is an Activity that extends Activity with android:theme="@android:style/Theme.Dialog" in the Manifest. I am using: 我正在尝试从弹出活动(该活动通过清单中的android:theme="@android:style/Theme.Dialog"扩展了Activity的活动)中向弹出窗口添加AlertDialog 。我正在使用:

Context context = getApplicationContext();
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Add Extra");
final EditText base = new EditText(builder.getContext());
final EditText value = new EditText(builder.getContext());
base.setHint("Name");
value.setHint("Value");
// Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
base.setInputType(InputType.TYPE_CLASS_TEXT);
value.setInputType(InputType.TYPE_CLASS_TEXT);
layout.addView(base);
layout.addView(value);
builder.setView(layout);
builder.setMessage("")
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {

       }
       })
       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
          // User cancelled the dialog
       }
       });
 // Create the AlertDialog object and return it
AlertDialog dialog = builder.create();
dialog.show();

to create my Alert, but I recieve an error about not having the correct context. 创建警报,但是我收到有关没有正确上下文的错误。 Is there a specific way to get the context of the popup dialog? 是否有获取弹出对话框上下文的特定方法?

You are using getApplicationContext(), use Activity context instead 您正在使用getApplicationContext(),而是使用Activity上下文

Use this instead to avoid errors 使用它来避免错误

Context context = YourCurrentActivity.this;

You should use the Activity context. 您应该使用Activity上下文。

Change this line, 更改此行,

Context context = getApplicationContext();

to this: 对此:

Context context = this;

Android AlertDialog in Activity Dialog.Theme 活动Dialog.Theme中的Android AlertDialog

I asked this question again and it was answered for anyone else having this problem. 我再次问了这个问题,其他任何有此问题的人都得到了回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM