按下后退键时无法显示我的对话框

时间:2013-06-03 07:27:56

标签: android android-alertdialog

我正在编写 BACK 按钮,因为我创建了一个警告对话框,以便在按下按钮进行确认时显示。但是我得到以下异常。

06-03 12:49:56.304: I/AFSDFDS(10186): Back Pressed
06-03 12:49:56.335: D/AndroidRuntime(10186): Shutting down VM
06-03 12:49:56.335: W/dalvikvm(10186): threadid=1: thread exiting with uncaught exception (group=0x40018578)
06-03 12:49:56.343: E/AndroidRuntime(10186): FATAL EXCEPTION: main
06-03 12:49:56.343: E/AndroidRuntime(10186): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
06-03 12:49:56.343: E/AndroidRuntime(10186):    at android.view.ViewRoot.setView(ViewRoot.java:536)
06-03 12:49:56.343: E/AndroidRuntime(10186):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
06-03 12:49:56.343: E/AndroidRuntime(10186):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
06-03 12:49:56.343: E/AndroidRuntime(10186):    at android.app.Dialog.show(Dialog.java:241)
06-03 12:49:56.343: E/AndroidRuntime(10186):    at com.example.nirbhaya.MainActivity.onBackPressed(MainActivity.java:174)
06-03 12:49:56.343: E/AndroidRuntime(10186):    at android.app.Activity.onKeyUp(Activity.java:1898)
06-03 12:49:56.343: E/AndroidRuntime(10186):    at android.view.KeyEvent.dispatch(KeyEvent.java:1294)
06-03 12:49:56.343: E/AndroidRuntime(10186):    at android.app.Activity.dispatchKeyEvent(Activity.java:2078)
06-03 12:49:56.343: E/AndroidRuntime(10186):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1741)
06-03 12:49:56.343: E/AndroidRuntime(10186):    at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2648)
06-03 12:49:56.343: E/AndroidRuntime(10186):    at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2623)
06-03 12:49:56.343: E/AndroidRuntime(10186):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1873)
06-03 12:49:56.343: E/AndroidRuntime(10186):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 12:49:56.343: E/AndroidRuntime(10186):    at android.os.Looper.loop(Looper.java:130)
06-03 12:49:56.343: E/AndroidRuntime(10186):    at android.app.ActivityThread.main(ActivityThread.java:3687)
06-03 12:49:56.343: E/AndroidRuntime(10186):    at java.lang.reflect.Method.invokeNative(Native Method)
06-03 12:49:56.343: E/AndroidRuntime(10186):    at java.lang.reflect.Method.invoke(Method.java:507)
06-03 12:49:56.343: E/AndroidRuntime(10186):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
06-03 12:49:56.343: E/AndroidRuntime(10186):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
06-03 12:49:56.343: E/AndroidRuntime(10186):    at dalvik.system.NativeStart.main(Native Method)

和我的java代码:

package com.example.nirbhaya;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    Button bdefsms,bsms,bmms,bemail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LinearLayout ll = (LinearLayout)findViewById(R.id.myLinear);
        //ll.setBackgroundColor(Color.RED);
        initialiasition();
    }

    private void initialiasition() {
        // TODO Auto-generated method stub

        bdefsms = (Button)findViewById(R.id.bDefSms);
        bsms = (Button)findViewById(R.id.bSMS);
        bmms = (Button)findViewById(R.id.bMMS);
        bemail = (Button)findViewById(R.id.bMail);

        bdefsms.setOnClickListener(this);
        bsms.setOnClickListener(this);
        bmms.setOnClickListener(this);
        bemail.setOnClickListener(this);


    }


    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
            .............
            ................
            .............

        }

    public void onBackPressed() {
    // do something on back.
    Log.i("AFSDFDS","Back Pressed");
    AlertDialog.Builder alertDialogBuilder2 = new AlertDialog.Builder(getBaseContext());
    alertDialogBuilder2.setTitle("EXIT");

    alertDialogBuilder2.setMessage("ARE YOU SURE?").setCancelable(false)

    .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int id) {
            finish();
        }
      })
    .setNegativeButton("No",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int id) {
            dialog.cancel();
        }
    });


    AlertDialog alertDialog2 = alertDialogBuilder2.create();
    alertDialog2.show();


    }   
}

处获得例外
  

alertDialog2.show();

2 个答案:

答案 0 :(得分:2)

你在这里收到错误。

AlertDialog.Builder alertDialogBuilder2 = new AlertDialog.Builder(getBaseContext());

应该是

AlertDialog.Builder alertDialogBuilder2 = new AlertDialog.Builder(this);

您应该在创建对话框时传递活动上下文。

答案 1 :(得分:0)

这是我通过使用后退按钮实现此操作以退出应用程序的方法:

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

            showExitDialog();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    private void showExitDialog() {
        // TODO Auto-generated method stub

        AlertDialog.Builder adb = new Builder(Home.this);
        adb.setTitle("Warning");
        adb.setMessage("Are you sure you want to quit ?");
        adb.setIcon(R.drawable.ic_launcher);
        adb.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {

                if (database.isOpen())
                    database.close();
                finish();
            }
        });

        adb.setNegativeButton("No", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface arg0, int arg1) {

                arg0.dismiss();
            }
        });

        AlertDialog ad = adb.create();
        ad.show();
    }