剛接觸Android並試圖重複調用invalidate函數。所以我選擇線程來做到這一點。我的線程運行正常,但試圖調用invalidate()時崩潰下面是代碼片段。在一個單獨的線程中調用無效時崩潰
class MyCanvas extends View {
private static final String TAG = "MY CANVAS EXAMPLE";
public MyCanvas(Context context){
super(context);
Log.e (TAG, " MY CANVAS CONSTRUCTOR GOT CALLED ");
// TODO Auto-generated constructor stub
Thread newBackground = new Thread (new Runnable(){
@Override
public void run() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
invalidate();
Log.e (TAG, " MY THREAD GOT CALLED ");
// TODO Auto-generated method stub
}
});
newBackground.start();
}
@Override
protected void onDraw(Canvas canvas) {
Log.e (TAG, " MY CANVAS GOT CALLED ");
// TODO Auto-generated method stub
super.onDraw(canvas);
}
}
和下面是崩潰日誌觀察
09-23 17:41:09.299: E/MY CANVAS EXAMPLE(1839): MY CANVAS CONSTRUCTOR GOT CALLED
09-23 17:41:09.599: E/MY CANVAS EXAMPLE(1839): MY CANVAS GOT CALLED
09-23 17:41:10.329: W/dalvikvm(1839): threadid=9: thread exiting with uncaught exception (group=0x40018578)
09-23 17:41:10.329: E/AndroidRuntime(1839): FATAL EXCEPTION: Thread-10
09-23 17:41:10.329: E/AndroidRuntime(1839): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
09-23 17:41:10.329: E/AndroidRuntime(1839): at android.view.ViewRoot.checkThread(ViewRoot.java:3020)
09-23 17:41:10.329: E/AndroidRuntime(1839): at android.view.ViewRoot.invalidateChild(ViewRoot.java:647)
09-23 17:41:10.329: E/AndroidRuntime(1839): at android.view.ViewRoot.invalidateChildInParent(ViewRoot.java:673)
09-23 17:41:10.329: E/AndroidRuntime(1839): at android.view.ViewGroup.invalidateChild(ViewGroup.java:2511)
09-23 17:41:10.329: E/AndroidRuntime(1839): at android.view.View.invalidate(View.java:5279)
09-23 17:41:10.329: E/AndroidRuntime(1839): at com.example.canvas.MainActivity$MyCanvas$1.run(MainActivity.java:83)
09-23 17:41:10.329: E/AndroidRuntime(1839): at java.lang.Thread.run(Thread.java:1019)