Android显示弹出活动的AlertDialog

Android显示弹出活动的AlertDialog,android,dialog,android-alertdialog,Android,Dialog,Android Alertdialog,我正在尝试添加一个AlertDialog以从弹出式活动中弹出(这是一个通过清单中的android:theme=“@android:style/theme.Dialog”扩展活动的活动)。我正在使用: Context context = getApplicationContext(); LinearLayout layout = new LinearLayout(context); layout.setOrientation(LinearLayout.VERTICAL); // Use the B

我正在尝试添加一个
AlertDialog
以从弹出式活动中弹出(这是一个通过清单中的
android:theme=“@android:style/theme.Dialog”
扩展活动的活动)。我正在使用:

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();

创建我的警报,但我收到一个关于没有正确上下文的错误。是否有特定的方法获取弹出对话框的上下文?

您应该使用活动上下文

换一行,

Context context = getApplicationContext();
为此:

Context context = this;

您正在使用getApplicationContext(),请改用活动上下文

使用此选项可以避免错误

Context context = YourCurrentActivity.this;


我又问了一次这个问题,其他有这个问题的人都得到了回答。

我收到了这个错误:
您需要使用一个Theme.AppCompat主题(或后代)使用此活动。
您的活动是从ActionBarActivity还是从AppCompatActivity扩展的?如果是,从activity扩展或此问题的任何其他答案可能会很方便。谢谢,我会看一看。我扩展activity而不是AppCompatActivity,因为我有主题对话框。这并不能回答AlertDialog不工作的原因关于这一点,你还有其他建议吗?