Android 显示AlertDialog正在使我的应用程序崩溃。

Android 显示AlertDialog正在使我的应用程序崩溃。,android,android-alertdialog,Android,Android Alertdialog,我创建了一个警报对话框,并将其放在按钮上OnClickListener。 但是,该对话框正在使我的应用程序崩溃 我的代码有什么问题 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Are you sure you want to exit?") .setCancelable(false) .setPositiveButton("Yes", new DialogInterf

我创建了一个
警报对话框
,并将其放在
按钮上
OnClickListener
。 但是,该对话框正在使我的应用程序崩溃

我的代码有什么问题

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?")
   .setCancelable(false)
   .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            MyActivity.this.finish();
       }
   })
   .setNegativeButton("No", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
       }
   });
AlertDialog alert = builder.create();
试试这个:

AlertDialog.Builder builder = new AlertDialog.Builder(Json_ReadActivity.this);
builder.setMessage("are you sure want to exit");
builder.setNeutralButton("Ok",new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(),"text",Toast.LENGTH_LONG).show();
    }
});

builder.show();

检查以下代码。它起作用了

   public class MainActivity extends Activity {
   CharSequence[] items = { “Google”, “Apple”, “Microsoft” };
        boolean[] itemsChecked = new boolean [items.length];
     /** Called when the activity is first created. */
      @Override
             public void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
              setContentView(R.layout.main);
             Button btn = (Button) findViewById(R.id.btn_dialog);
               btn.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {
            showDialog(0);
       }
         });
     }
      @Override
         protected Dialog onCreateDialog(int id) {
         switch (id) {
         case 0:
          return new AlertDialog.Builder(this)
       .setIcon(R.drawable.icon)
       .setTitle("This is a dialog with some simple text...")
         .setPositiveButton("OK", new
        DialogInterface.OnClickListener() {
          public void onClick(DialogInterface dialog,
            int whichButton)
      {
        Toast.makeText(getBaseContext(),
                               "OK clicked!", Toast.LENGTH_SHORT).show();
          }
         })
    .setNegativeButton("Cancel", new
    DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog,
     int whichButton)
     {
       Toast.makeText(getBaseContext(),
       "Cancel clicked!", Toast.LENGTH_SHORT).show();
       }
      })
      )
     .create();
     }
    return null;
       }
      }
尝试添加

dialog.dismiss();
以前

MyActivity.this.finish();

你在logcat中看到了什么?有例外吗?当您试图显示对话框或与之交互时,应用程序是否崩溃?您的代码看起来不错,但它需要更多的上下文。我建议发布更多的活动代码会有所帮助,至少是onCreate()和onCreateDialog()方法。此外,正如马杜山所提到的,logcat的内容也将为原因提供强有力的线索。