2011-11-09 27 views
0

我知道停止方法已被棄用,我現在使用的破壞方法,但是我得到這個錯誤:android如何停止或銷燬一個線程?

11-09 11:42:28.740: E/AndroidRuntime(1538): FATAL EXCEPTION: main 
11-09 11:42:28.740: E/AndroidRuntime(1538): java.lang.NoSuchMethodError: Thread.destroy() 
11-09 11:42:28.740: E/AndroidRuntime(1538):  at java.lang.Thread.destroy(Thread.java:600) 
11-09 11:42:28.740: E/AndroidRuntime(1538):  at com.rathbones.src.NewslettersActivity.onKeyDown(NewslettersActivity.java:144) 
11-09 11:42:28.740: E/AndroidRuntime(1538):  at android.view.KeyEvent.dispatch(KeyEvent.java:1037) 
11-09 11:42:28.740: E/AndroidRuntime(1538):  at android.app.Activity.dispatchKeyEvent(Activity.java:2068) 
11-09 11:42:28.740: E/AndroidRuntime(1538):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1643) 
11-09 11:42:28.740: E/AndroidRuntime(1538):  at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2471) 
11-09 11:42:28.740: E/AndroidRuntime(1538):  at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2441) 
11-09 11:42:28.740: E/AndroidRuntime(1538):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1735) 
11-09 11:42:28.740: E/AndroidRuntime(1538):  at android.os.Handler.dispatchMessage(Handler.java:99) 
11-09 11:42:28.740: E/AndroidRuntime(1538):  at android.os.Looper.loop(Looper.java:123) 
11-09 11:42:28.740: E/AndroidRuntime(1538):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
11-09 11:42:28.740: E/AndroidRuntime(1538):  at java.lang.reflect.Method.invokeNative(Native Method) 
11-09 11:42:28.740: E/AndroidRuntime(1538):  at java.lang.reflect.Method.invoke(Method.java:521) 
11-09 11:42:28.740: E/AndroidRuntime(1538):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
11-09 11:42:28.740: E/AndroidRuntime(1538):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
11-09 11:42:28.740: E/AndroidRuntime(1538):  at dalvik.system.NativeStart.main(Native Method) 
11-09 11:42:28.760: W/ActivityManager(59): Force finishing activity com.rathbones.src/.NewslettersActivity 

但應用程序沒有崩潰,它只是我得到了logcat的這個錯誤。 其實我有一個時事通訊模塊,它使用戶能夠查看PDF文件,當他們按下視圖按鈕時,它打開一個進度條,並在同一時間,如果有人按下後按鈕,它應該停止線程並優雅地退出。它這樣做,但在日誌貓我得到上述錯誤。這是導致此錯誤的代碼片段:

private void viewOnline() { 

     if (currentNewsletter == null) { 
      Log.e(Constants.APP_NAME, "No newsletter selected"); 
      return; 
     } 

     final ProgressDialog d = new ProgressDialog(this); 
     d.setMessage("Downloading..."); 
     d.show(); 

     final Context context = getApplicationContext(); 
     t = new Thread(new Runnable() { 
      public void run() { 

       String fileName = currentNewsletter.mFilename; 

       Log.d(Constants.APP_NAME, "Downloading/showing: " + fileName); 
       final File file = Utilities.getFileFromURL(context, currentNewsletter.mUrl, currentNewsletter.mExpectedSizeInBytes, fileName, false); 

       d.dismiss(); 
       // Now we can show the file 
       viewPDF(file); 
      } 
     }); 
     t.start(); 

     // Utilities.List(getApplicationContext().getFilesDir().getPath()); 
     // Utilities.List(getApplicationContext().getDir("files", Context.MODE_WORLD_WRITEABLE).getAbsolutePath()); 
     // Utilities.DeleteDirectory(getApplicationContext().getDir("files", Context.MODE_WORLD_WRITEABLE).getAbsolutePath()); 

    } 

    private void viewPDF(File file) { 

     //DEBUG DEBUG DEBUG 
     //Log.d(Constants.APP_NAME, "ViewPDF: showing " + file.getName()); 
     //Log.d(Constants.APP_NAME, "Path: " + file.getPath()); 
     //Log.d(Constants.APP_NAME, "Exists: " + file.exists()); 
     //Log.d(Constants.APP_NAME, "Length: " + file.length()); 
     //DEBUG DEBUG DEBUG 

     // Now it's all safe and sound and local, open it 
     Intent intent = new Intent(Intent.ACTION_VIEW); 
     intent.setDataAndType(Uri.fromFile(file), "application/pdf"); 

     try { 
      startActivity(intent); 
     } catch (Exception e) { 
      Toast.makeText(this, "No Application Available to View PDF", Toast.LENGTH_SHORT).show(); 
     } 
    } 

    @Override 
    protected void onStop() { 
     finish(); 
     super.onStop(); 
    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_BACK) { 
      t.destroy(); 
      Intent i = new Intent(NewslettersActivity.this,MainMenuActivity.class); 

      startActivity(i); 
      finish(); 
      return true; 
     } 
     return super.onKeyDown(keyCode, event); 
    } 
+0

銷燬也被棄用。請點擊此鏈接:http://developer.android.com/reference/java/lang/Thread.html#destroy%28%29 –

+0

@VineetShukla謝謝你讓我知道。 – Maverick

回答

3

如果你有一個while loop裏面的線程,你可以通過boolean flag來控制這個線程的while條件。當您設置flag to false時,線程剛剛完成其任務

編輯:小例子,

boolean flag = true; 
Thread secondary = new Thread(new Runnable() { 

@Override 
public void run() { 
    while (flag) { 
    // do something 
    } 
} 
}); 

secondary.start(); //start the thread 
flag = false; // this will force secondary to finish its execution 
try { 
    secondary.join(); // wait for secondary to finish 
    } catch (InterruptedException e) { 
    throw new RuntimeException(e); 
} 

我會發現這個代碼SO,它也爲我工作。

+0

請你更詳細地告訴我。 – Maverick

+0

好..它看起來是一個非常合理的解決方案..只是現在就嘗試它..並會讓你知道 – Maverick

+0

nope..its不工作..它崩潰的應用程序 – Maverick

1

使用中斷而不是銷燬。

+0

沒有..給錯誤。實際上導致應用程序崩潰。 – Maverick

+0

然後按照這個:http://stackoverflow.com/questions/5241822/is-it-good-way-to-stop-java-thread-forcefully/5241925#5241925 –

0

到user370305答案,也許2點的變化可以幫助:

1)使用AtomicBoolean代替布爾值,如果線程在另一個核上運行,變化不能作爲標誌可見。

2)刪除catch中的throw new RuntimeException(e)。它會在您拋出異常時崩潰。