*所以我有一个拥有 sleep 方法的线程,我认为这是问题所在,因为当我尝试使用 stop() 方法停止它时,我的程序崩溃了。当用户点击后退按钮(硬件后退按钮)时,我试图完全停止线程。我该怎么办?我应该使用睡眠方法以外的方法吗?*
Thread timer2 = new Thread() { // Threads - do multiple things
public void run() {
try {
sleep(5000); // 5000 mil secs = 5 secs . sleeps thread
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
cdt2.start();
}
}
};
timer2.start(); // starts thread
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
timer1.stop(); //error
timer2.stop(); //error
cdt.cancel();
cdt2.cancel();
Intent menu = new Intent("android.intent.action.MENU");
startActivity(menu);
return true;
}
return false;
}
日志猫:
06-20 13:08:58.006: D/dalvikvm(22050): GC_CONCURRENT freed 951K, 7% free 15393K/16455K, paused 2ms+3ms
06-20 13:08:58.944: D/dalvikvm(22050): GC_CONCURRENT freed 700K, 6% free 15533K/16455K, paused 2ms+1ms
06-20 13:09:01.733: D/dalvikvm(22050): GC_CONCURRENT freed 955K, 7% free 15630K/16647K, paused 2ms+2ms
06-20 13:09:02.069: D/dalvikvm(22050): GC_CONCURRENT freed 880K, 7% free 15707K/16711K, paused 2ms+2ms
06-20 13:09:02.678: D/dalvikvm(22050): GC_CONCURRENT freed 762K, 5% free 15940K/16775K, paused 2ms+2ms
06-20 13:09:03.303: D/AccountTypeManager(21214): Registering external account type=ro.weednet.contactssync, packageName=ro.weednet.contactssync
06-20 13:09:03.311: D/AccountTypeManager(21214): Registering external account type=com.skype.contacts.sync, packageName=com.skype.raider
06-20 13:09:03.311: W/ResourceType(21214): getEntry failing because entryIndex 1377 is beyond type entryCount 189
06-20 13:09:03.311: W/ResourceType(21214): Failure getting entry for 0x7f020561 (t=1 e=1377) in package 0 (error -2147483647)
06-20 13:09:03.311: W/ResourceType(21214): getEntry failing because entryIndex 1378 is beyond type entryCount 189
06-20 13:09:03.311: W/ResourceType(21214): Failure getting entry for 0x7f020562 (t=1 e=1378) in package 0 (error -2147483647)
06-20 13:09:03.311: W/ResourceType(21214): getEntry failing because entryIndex 1379 is beyond type entryCount 189
06-20 13:09:03.311: W/ResourceType(21214): Failure getting entry for 0x7f020563 (t=1 e=1379) in package 0 (error -2147483647)
06-20 13:09:03.311: W/ResourceType(21214): getEntry failing because entryIndex 1778 is beyond type entryCount 189
06-20 13:09:03.311: W/ResourceType(21214): Failure getting entry for 0x7f0206f2 (t=1 e=1778) in package 0 (error -2147483647)
06-20 13:09:03.311: D/AccountTypeManager(21214): Registering external account type=com.yahoo.mobile.client.share.sync, packageName=com.yahoo.mobile.client.android.mail
06-20 13:09:03.319: D/AccountTypeManager(21214): Registering external account type=com.facebook.auth.login, packageName=com.facebook.katana
06-20 13:09:03.319: W/ResourceType(21214): getEntry failing because entryIndex 344 is beyond type entryCount 189