我知道停止方法已被弃用,我现在正在使用destroy方法,但是我收到了这个错误:
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 :(得分: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中找到这个代码,它也适用于我。
答案 1 :(得分:1)
使用中断而不是销毁。
答案 2 :(得分:0)
对于user370305回答,可能有2个更改可能会有所帮助:
1)使用AtomicBoolean
代替布尔值,如果线程在另一个核心中运行,则标志无法看到更改。
2)删除捕获中的throw new RuntimeException(e)
。当你抛出异常时它会崩溃。