0
public class MainActivity extends Activity implements OnDoubleTapListener,OnGestureListener
    {
        GestureDetector detector;
            @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
           detector = new GestureDetector(this, this);
        }

        @Override
        public boolean onDoubleTap(MotionEvent e) {
            if(e.getAction()==1)
            {
            //Toast.makeText(getBaseContext(), "onDoubleTap", Toast.LENGTH_LONG).show();
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    getApplicationContext());

                // set title
                alertDialogBuilder.setTitle("Your Title");

                // set dialog message
                alertDialogBuilder
                    .setMessage("Click yes to exit!")
                    .setCancelable(false)
                    .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                            // if this button is clicked, close
                            // current activity
                            MainActivity.this.finish();
                        }
                      })
                    .setNegativeButton("No",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                            // if this button is clicked, just close
                            // the dialog box and do nothing
                            dialog.cancel();
                        }
                    });

                    // create alert dialog
                    AlertDialog alertDialog = alertDialogBuilder.create();

                    // show it
                    alertDialog.show();
                }

            return true;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            if(e.getAction()==1)
            {
           // Toast.makeText(getBaseContext(), "onDoubleTapEventOccursAgainAndAgain", Toast.LENGTH_LONG).show();
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    getApplicationContext());

                // set title
                alertDialogBuilder.setTitle("Your Title");

                // set dialog message
                alertDialogBuilder
                    .setMessage("Click yes to exit!")
                    .setCancelable(false)
                    .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                            // if this button is clicked, close
                            // current activity
                            MainActivity.this.finish();
                        }
                      })
                    .setNegativeButton("No",new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int id) {
                            // if this button is clicked, just close
                            // the dialog box and do nothing
                            dialog.cancel();
                        }
                    });

                    // create alert dialog
                    AlertDialog alertDialog = alertDialogBuilder.create();

                    // show it
                    alertDialog.show();
                }



            return true;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            if(e.getAction()==1)
            {
            Toast.makeText(getBaseContext(), "onSingleTapConfirmed", Toast.LENGTH_LONG).show();
            }
            return true;
        }

        @Override
        public boolean onDown(MotionEvent e) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public void onLongPress(MotionEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
                float distanceY) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public void onShowPress(MotionEvent e) {
            // TODO Auto-generated method stub

        }

        @Override
        public boolean onSingleTapUp(MotionEvent e) {
            // TODO Auto-generated method stub
            return false;
        }
        public boolean onTouchEvent(MotionEvent event) {
            return detector.onTouchEvent(event);
        }
    }

在这个我想在我双击android屏幕时创建警报对话框。但我发现以下错误:

06-03 11:26:13.321: E/AndroidRuntime(12824): FATAL EXCEPTION: main
06-03 11:26:13.321: E/AndroidRuntime(12824): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
4

2 回答 2

0

能不能换个试试...

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext());

有了这个

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity()); 

因为我看不到你的 logcat 的其余部分,我猜这是错误。

于 2013-06-10T14:23:39.473 回答
0

代替

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getApplicationContext());

进入

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(YourActivityName.this);
于 2013-06-10T14:30:55.653 回答