Android停止线程onDestroy

时间:2014-11-09 13:33:03

标签: java android multithreading destroy

我很想在android中进行线程处理,只是弄脏我的手。每当我尝试停止onDestroy();

进程时,我都会一直遇到nullpointerexception错误

logcat的

11-09 08:23:46.776: W/dalvikvm(2869): threadid=1: thread exiting with uncaught exception (group=0x41ec6e48)
11-09 08:23:46.796: E/AndroidRuntime(2869): FATAL EXCEPTION: main
11-09 08:23:46.796: E/AndroidRuntime(2869): Process: com.rhynoapplications.threadingtutorial, PID: 2869
11-09 08:23:46.796: E/AndroidRuntime(2869): java.lang.RuntimeException: Unable to destroy activity {com.rhynoapplications.threadingtutorial/com.rhynoapplications.threadingtutorial.MainActivity}: java.lang.NullPointerException
11-09 08:23:46.796: E/AndroidRuntime(2869):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3514)
11-09 08:23:46.796: E/AndroidRuntime(2869):     at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3532)
11-09 08:23:46.796: E/AndroidRuntime(2869):     at android.app.ActivityThread.access$1400(ActivityThread.java:139)
11-09 08:23:46.796: E/AndroidRuntime(2869):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1253)
11-09 08:23:46.796: E/AndroidRuntime(2869):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-09 08:23:46.796: E/AndroidRuntime(2869):     at android.os.Looper.loop(Looper.java:136)
11-09 08:23:46.796: E/AndroidRuntime(2869):     at android.app.ActivityThread.main(ActivityThread.java:5105)
11-09 08:23:46.796: E/AndroidRuntime(2869):     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 08:23:46.796: E/AndroidRuntime(2869):     at java.lang.reflect.Method.invoke(Method.java:515)
11-09 08:23:46.796: E/AndroidRuntime(2869):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
11-09 08:23:46.796: E/AndroidRuntime(2869):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
11-09 08:23:46.796: E/AndroidRuntime(2869):     at dalvik.system.NativeStart.main(Native Method)
11-09 08:23:46.796: E/AndroidRuntime(2869): Caused by: java.lang.NullPointerException
11-09 08:23:46.796: E/AndroidRuntime(2869):     at com.rhynoapplications.threadingtutorial.MainActivity.onDestroy(MainActivity.java:181)
11-09 08:23:46.796: E/AndroidRuntime(2869):     at android.app.Activity.performDestroy(Activity.java:5447)
11-09 08:23:46.796: E/AndroidRuntime(2869):     at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1117)
11-09 08:23:46.796: E/AndroidRuntime(2869):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3501)
11-09 08:23:46.796: E/AndroidRuntime(2869):     ... 11 more
11-09 08:25:25.566: I/Process(2869): Sending signal. PID: 2869 SIG: 9
11-09 08:25:31.086: I/ActivityManager(4797): Timeline: Activity_idle id: android.os.BinderProxy@42f042b0 time:120024491
11-09 08:25:31.706: I/ViewRootImpl(4797): ViewRoot's KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_BACK, scanCode=0, metaState=0, flags=0x48, repeatCount=0, eventTime=120025111, downTime=120025111, deviceId=-1, source=0x101 } to com.android.internal.policy.impl.PhoneWindow$DecorView{42f12b08 V.E..... R....... 0,0-1080,1776}
11-09 08:25:31.796: I/ViewRootImpl(4797): ViewRoot's KeyEvent { action=ACTION_UP, keyCode=KEYCODE_BACK, scanCode=0, metaState=0, flags=0x48, repeatCount=0, eventTime=120025202, downTime=120025111, deviceId=-1, source=0x101 } to com.android.internal.policy.impl.PhoneWindow$DecorView{42f12b08 V.E..... R....... 0,0-1080,1776}
11-09 08:25:32.396: D/AndroidRuntime(4797): Shutting down VM
11-09 08:25:32.396: W/dalvikvm(4797): threadid=1: thread exiting with uncaught exception (group=0x41ec6e48)
11-09 08:25:32.396: E/AndroidRuntime(4797): FATAL EXCEPTION: main
11-09 08:25:32.396: E/AndroidRuntime(4797): Process: com.rhynoapplications.threadingtutorial, PID: 4797
11-09 08:25:32.396: E/AndroidRuntime(4797): java.lang.RuntimeException: Unable to destroy activity {com.rhynoapplications.threadingtutorial/com.rhynoapplications.threadingtutorial.MainActivity}: java.lang.NullPointerException
11-09 08:25:32.396: E/AndroidRuntime(4797):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3514)
11-09 08:25:32.396: E/AndroidRuntime(4797):     at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3532)
11-09 08:25:32.396: E/AndroidRuntime(4797):     at android.app.ActivityThread.access$1400(ActivityThread.java:139)
11-09 08:25:32.396: E/AndroidRuntime(4797):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1253)
11-09 08:25:32.396: E/AndroidRuntime(4797):     at android.os.Handler.dispatchMessage(Handler.java:102)
11-09 08:25:32.396: E/AndroidRuntime(4797):     at android.os.Looper.loop(Looper.java:136)
11-09 08:25:32.396: E/AndroidRuntime(4797):     at android.app.ActivityThread.main(ActivityThread.java:5105)
11-09 08:25:32.396: E/AndroidRuntime(4797):     at java.lang.reflect.Method.invokeNative(Native Method)
11-09 08:25:32.396: E/AndroidRuntime(4797):     at java.lang.reflect.Method.invoke(Method.java:515)
11-09 08:25:32.396: E/AndroidRuntime(4797):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
11-09 08:25:32.396: E/AndroidRuntime(4797):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
11-09 08:25:32.396: E/AndroidRuntime(4797):     at dalvik.system.NativeStart.main(Native Method)
11-09 08:25:32.396: E/AndroidRuntime(4797): Caused by: java.lang.NullPointerException
11-09 08:25:32.396: E/AndroidRuntime(4797):     at com.rhynoapplications.threadingtutorial.MainActivity.onDestroy(MainActivity.java:180)
11-09 08:25:32.396: E/AndroidRuntime(4797):     at android.app.Activity.performDestroy(Activity.java:5447)
11-09 08:25:32.396: E/AndroidRuntime(4797):     at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1117)
11-09 08:25:32.396: E/AndroidRuntime(4797):     at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3501)
11-09 08:25:32.396: E/AndroidRuntime(4797):     ... 11 more

这是我目前正在使用的代码。

class MyThreadRunner extends Thread {
        // @Override
     int count = 0;
     boolean stopThread = false;
        public void run() {
            while (count < 90 && !stopThread) {
            try {
                if(count >= 90){
                    stopThread = true;
                }
                //500 is half a second...
                int sleepTime = count < 80 ? 10 : 30;
                Thread.sleep(sleepTime);
                count += 1;
                 progressBar.setProgress(count);
                 bar.setProgress(count);
                 progressBar.setText(count + "%");
            } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
        }
        };

        public void cancel(){
            stopThread = true;
        }
 }; 

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    newThread.cancel();
    super.onDestroy();
}

1 个答案:

答案 0 :(得分:1)

尝试使用interrupt方法完全停止thread而不是cancel

newThread.interrupt();

请注意,如果您interrupt / onPause中有onStop个帖子,则可能需要在thread / {{1}中再次创建onRestart }。