0

我是 Android 的新手,我创建了一个应用程序,但是当我在单击按钮时开始相同的活动时它崩溃了,我也完成了当前Activity这是我的错误日志。我Timer也在我的应用程序和背景音乐中使用

12-18 05:55:15.776: D/AndroidRuntime(2345): Shutting down VM
12-18 05:55:15.776: W/dalvikvm(2345): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
12-18 05:55:15.817: E/AndroidRuntime(2345): FATAL EXCEPTION: main
12-18 05:55:15.817: E/AndroidRuntime(2345): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@41542480 is not valid; is your activity running?
12-18 05:55:15.817: E/AndroidRuntime(2345):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:585)
12-18 05:55:15.817: E/AndroidRuntime(2345):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
12-18 05:55:15.817: E/AndroidRuntime(2345):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
12-18 05:55:15.817: E/AndroidRuntime(2345):     at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
12-18 05:55:15.817: E/AndroidRuntime(2345):     at android.view.Window$LocalWindowManager.addView(Window.java:547)
12-18 05:55:15.817: E/AndroidRuntime(2345):     at android.app.Dialog.show(Dialog.java:277)
12-18 05:55:15.817: E/AndroidRuntime(2345):     at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
12-18 05:55:15.817: E/AndroidRuntime(2345):     at com.example.whowantto.play$timer.onFinish(play.java:722)
12-18 05:55:15.817: E/AndroidRuntime(2345):     at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118)
12-18 05:55:15.817: E/AndroidRuntime(2345):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 05:55:15.817: E/AndroidRuntime(2345):     at android.os.Looper.loop(Looper.java:137)
12-18 05:55:15.817: E/AndroidRuntime(2345):     at android.app.ActivityThread.main(ActivityThread.java:4745)
12-18 05:55:15.817: E/AndroidRuntime(2345):     at java.lang.reflect.Method.invokeNative(Native Method)
12-18 05:55:15.817: E/AndroidRuntime(2345):     at java.lang.reflect.Method.invoke(Method.java:511)
12-18 05:55:15.817: E/AndroidRuntime(2345):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-18 05:55:15.817: E/AndroidRuntime(2345):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-18 05:55:15.817: E/AndroidRuntime(2345):     at dalvik.system.NativeStart.main(Native Method)
12-18 05:55:57.476: E/Trace(2564): error opening trace file: No such file or directory (2)
12-18 05:55:57.546: D/AndroidRuntime(2564): Shutting down VM

提前致谢

4

2 回答 2

0

似乎您正试图Dialog在您调用finish()当前Activity

于 2012-12-17T12:45:38.700 回答
0

CountDownTimer尝试在其生命周期结束AlertDialog后显示Activity。也许你在打电话finish(),但不是cancel()任务。

您可以添加一个由计时器检查的变量,如果它仍应显示如下示例中的对话框

public class TimerActivity extends Activity {

    private boolean mDialogEnabled;

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

        // add timer start to some button
        findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new CountDownTimer(5000, 1000) {
                    @Override
                    public void onTick(long millisUntilFinished) {
                        // something
                    }

                    @Override
                    public void onFinish() {
                        if (mDialogEnabled) // <<< check
                            new AlertDialog.Builder(TimerActivity.this)
                                .show();
                    }
                }.start();
            }
        });
    }

    // enable/disable showing the dialog in start / stop for example

    @Override
    protected void onStart() {
        super.onStart();
        mDialogEnabled = true;
    }

    @Override
    protected void onStop() {
        super.onStop();
        mDialogEnabled = false;
    }
}

或者您可以使用更复杂的实现,该实现应该保留计时器任务并将其重新附加到下一个活动实例,如下例所示。例如,通过这种方式,可以在旋转时重新创建 Activity,并且仍然使用在旧 Activity 实例中启动的计时器。

public class TimerActivity extends Activity {

    static class MyCountDownTimer extends CountDownTimer {
        private Context mContext;
        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        public void setContext(Context context) {
            mContext = context;
        }

        @Override
        public void onFinish() {
            if (mContext != null) {
                new AlertDialog.Builder(mContext)
                    .setMessage("Finished!")
                    .show();
            }
        }
        @Override
        public void onTick(long millisUntilFinished) {
            if (mContext != null) {
                // do something
            }
        }
    }

    MyCountDownTimer mTimer;

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

        // restore timer to current Activity
        mTimer = (MyCountDownTimer) getLastNonConfigurationInstance();

        // add timer start to some button
        findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // cancel old timers
                if (mTimer != null) {
                    mTimer.cancel();
                }
                mTimer = new MyCountDownTimer(5000, 1000);
                mTimer.start();
            }
        });

    }

    @Override
    public Object onRetainNonConfigurationInstance() {
        // save timer and detach from Activity
        if (mTimer != null) {
            mTimer.setContext(null);
        }
        return mTimer;
    }

    @Override
    protected void onStart() {
        super.onStart();
        // Activity going to be visible now, attach timer
        if (mTimer != null)
            mTimer.setContext(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        // Activity going to be invisible, detach timer
        if (mTimer != null) {
            mTimer.setContext(null);
            // if activity is not to be recreated cancel it
            if (isFinishing()) {
                mTimer.cancel();
                mTimer = null;
            }
        }
    }
}

所有代码都未经测试,但大致是它应该如何工作。

于 2012-12-17T14:54:03.160 回答