2014-11-09 12 views
0

나는 안드로이드에서 스레딩하기에 매우 새롭다. 단지 내 손을 더럽힐 뿐이다. 내가 onClick 언제든 nullpointerexception 오류가 계속 발생합니다. onDestroy();안드로이드 스톱 스레드 onDestroy

로그 캣 여기

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: [email protected] 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 

내가 현재 사용하고 코드입니다. 당신이 onPause/onStop에서 스레드를 interrupt 경우, 당신은 onRestart에 다시 thread을 만들어야 할 수도 있음을

newThread.interrupt(); 

참고 :

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

시도는 완전히 thread 대신 cancel의 중지 interrupt 방법을 사용하는/onStart.

+0

덕분에, 이것은 완벽하게 작동했습니다. – Rhynoboy

+0

이것은 우리가 앱을 나갈 때만 문제를 해결합니다. – Sam