由於某種原因,我的程序在完成thread.start方法後崩潰。由於這是我第一次使用線程,我完全迷失了方向,不知道發生了什麼。thread.start後Android java崩潰
public void countingDown() {
waitingTime = (long) (Math.random() * 1000 + 3000);
button.setText("Wait");
try {
Thread.sleep(waitingTime);
} catch (Exception e) {
}
time1 = System.currentTimeMillis();
button.setText("Press");}
,並呼籲輪距:
new Thread(new Runnable() {
@Override
public void run() {
countingDown();
}
}).start();
我會很感激!
我的logcat顯示以下(抱歉混亂):
10-22 14:59:43.319:E/AndroidRuntime(961):致命異常:螺紋81 10-22 14: 59:43.319:E/AndroidRuntime(961):android.view.ViewRootImpl $ CalledFromWrongThreadException:只有創建視圖層次結構的原始線程可以觸及其視圖。 E/AndroidRuntime(961):at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:5908) 10-22 14:59:43.319:E/AndroidRuntime(961):at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:869) 10-22 14:59:43.319:E/AndroidRuntime(961):at android.view.ViewGroup.invalidateChild(ViewGroup.java:4253) 10-22 14:59:43.319:E/AndroidRuntime(961):在android.view.View.invalidate(View.java:10539) 10-22 14:59:43.319:E/AndroidRuntime(961):在android.view。 View.invalidate(View.java:10494) 10-22 14:59:43.319:E/AndroidRuntime(961):at android.widget.TextView.checkForRelayout(TextView.java:6511) 10-22 14:59: 43.319:E/AndroidRuntime(961):在android.widget.TextView.setText(TextView.java:3771) 10-22 14:59:43.319:E/AndroidRuntime(961):在android.widget.TextView.setText(TextView.java:3629) 10-22 14:59:43.319:E/AndroidRuntime(961):在android.widget.TextView.setText(TextView.java: 3604) 10-22 14:59:43.319:E/AndroidRuntime(961):在week8.handin.Week8Handin.countingDown(Week8Handin.java:59) 10-22 14:59:43.319:E/AndroidRuntime(961) :在week8.handin.Week8Handin $ 1.run(Week8Handin.java:82) 10-22 14:59:43.319:E/AndroidRuntime(961):at java.lang.Thread.run(Thread.java:841)
Line,Exception !! – hasan83
錯誤是關於訪問'button'變量不是線程安全的,有沒有其他線程運行花花公子? – 2013-10-22 19:14:15
這是正確的。您只能訪問主UI線程中的按鈕。 –