在Android中完成count_downtimer时显示自定义对话框消息

时间:2013-11-20 11:28:29

标签: java android eclipse

我正在为有趣的游戏实施倒数计时器。我想在完成一分钟后显示自定义对话框。我试过,当运行应用程序时它运行正常,但我得到了例外,例如FATAL EXCEPTION。我每次都尝试过很多例外。

以下是我的活动中倒数计时器的代码。

new CountDownTimer(60*1*1000, 1000) 
        {

            @Override
            public void onTick(long millisUntilFinished) 
            {
                textView_TimerValue.setText("seconds remaining : "
                        + millisUntilFinished / 1000);
            }

       @Override
       public void onFinish() 
            {

           textView_TimerValue.setText("TIME IS OVER");
            //tell the Dialog to use the dialog.xml as it's layout description
                dialog = new Dialog(Gsk_Demo.this);

                dialog.setContentView(R.layout.dialogbox_);

                TextView txt = (TextView) dialog.findViewById(R.id.textV);
                txt.setText("Your Time Is Over.Click Next.");

                Button dialogButton1 = (Button) dialog.findViewById(R.id.buttonDialog_Next);

                dialogButton1.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent i=new Intent(Gsk_Demo.this , Code_Demo.class);
                        startActivity(i);

                    }
                });

                dialog.show();
            }
        }.start();

这是我的Log cat堆栈跟踪。

11-20 15:22:44.158: E/AndroidRuntime(1211): FATAL EXCEPTION: main
11-20 15:22:44.158: E/AndroidRuntime(1211): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@4059c040 is not valid; is your activity running?
11-20 15:22:44.158: E/AndroidRuntime(1211):     at android.view.ViewRoot.setView(ViewRoot.java:527)
11-20 15:22:44.158: E/AndroidRuntime(1211):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
11-20 15:22:44.158: E/AndroidRuntime(1211):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
11-20 15:22:44.158: E/AndroidRuntime(1211):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
11-20 15:22:44.158: E/AndroidRuntime(1211):     at android.app.Dialog.show(Dialog.java:241)
11-20 15:22:44.158: E/AndroidRuntime(1211):     at com.gsk.Gsk_Demo$1.onFinish(Gsk_Demo.java:124)
11-20 15:22:44.158: E/AndroidRuntime(1211):     at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118)
11-20 15:22:44.158: E/AndroidRuntime(1211):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-20 15:22:44.158: E/AndroidRuntime(1211):     at android.os.Looper.loop(Looper.java:123)
11-20 15:22:44.158: E/AndroidRuntime(1211):     at android.app.ActivityThread.main(ActivityThread.java:3683)
11-20 15:22:44.158: E/AndroidRuntime(1211):     at java.lang.reflect.Method.invokeNative(Native Method)
11-20 15:22:44.158: E/AndroidRuntime(1211):     at java.lang.reflect.Method.invoke(Method.java:507)
11-20 15:22:44.158: E/AndroidRuntime(1211):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-20 15:22:44.158: E/AndroidRuntime(1211):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-20 15:22:44.158: E/AndroidRuntime(1211):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

试试这个希望这对你有用

final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.camgaldialog);
                dialog.setTitle("Complete action using");
                Button dialogcam = (Button) dialog.findViewById(R.id.Bcam);
                dialogcam.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View view0) {
                        // TODO Auto-generated method stub
                        Intent i = new Intent(
                                android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        startActivityForResult(i, 0);
                        dialog.dismiss();
                    }

                });
                dialog.show();
                break;