我正在为有趣的游戏实现倒数计时器。一分钟结束后,我想显示自定义对话框。我尝试过,当运行该应用程序时,它可以正常运行,但出现致命异常之类的异常。我尝试了很多,每次都会得到例外。
这是我的活动中倒数计时器的代码。
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)
最佳答案
试试这个希望这对你有用
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;