2017-05-08 97 views
0

我有一個按鈕視圖,我想禁止與創建它的線程不同的線程。我收到以下錯誤:Android:基於從其他線程觸發的事件更新GUI

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 
Process: org.pcgod.mumbleclient, PID: 21302 
java.lang.RuntimeException: An error occurred while executing doInBackground() 
at android.os.AsyncTask$3.done(AsyncTask.java:309) 
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354) 
at java.util.concurrent.FutureTask.setException(FutureTask.java:223) 
at java.util.concurrent.FutureTask.run(FutureTask.java:242) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
at java.lang.Thread.run(Thread.java:818) 
Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views. 
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:8129) 
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1258) 
at android.view.ViewGroup.invalidateChild(ViewGroup.java:5430) 
at android.view.View.invalidateInternal(View.java:13927) 
at android.view.View.invalidate(View.java:13863) 
at android.view.View.invalidateDrawable(View.java:18097) 
at android.widget.TextView.invalidateDrawable(TextView.java:6177) 
at android.graphics.drawable.DrawableContainer.invalidateDrawable(DrawableContainer.java:377) 
at android.graphics.drawable.Drawable.invalidateSelf(Drawable.java:844) 
at android.graphics.drawable.Drawable.setVisible(Drawable.java:1575) 
at android.graphics.drawable.DrawableContainer.selectDrawable(DrawableContainer.java:448) 
at android.graphics.drawable.StateListDrawable.onStateChange(StateListDrawable.java:104) 
at android.graphics.drawable.Drawable.setState(Drawable.java:1413) 
at android.view.View.drawableStateChanged(View.java:18264) 
at android.widget.TextView.drawableStateChanged(TextView.java:4505) 
at android.view.View.refreshDrawableState(View.java:18328) 
at android.view.View.setEnabled(View.java:8018) 
at android.widget.TextView.setEnabled(TextView.java:1895) 
at org.pcgod.mumbleclient.Matar.TcpClient.readMessageFromServer(TcpClient.java:103) 
at org.pcgod.mumbleclient.Matar.home$requestsQueueConnectTask.doInBackground(home.java:297) 
at org.pcgod.mumbleclient.Matar.home$requestsQueueConnectTask.doInBackground(home.java:276) 
at android.os.AsyncTask$2.call(AsyncTask.java:295) 
at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)  
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)  
at java.lang.Thread.run(Thread.java:818)  

這裏是我的代碼:

requestBTN = (Button) findViewById(R.id.requestBTN); 
new requestsQueueConnectTask().execute(""); //handle requests queueing 

如何解決此問題?我需要根據從其他線程觸發的事件更新GUI(Asynctask

回答

1

當您使用AsyncTask時,它將按順序執行4個步驟。
1. onPreExecute()
2. doInBackground()
3. onProgressUpdate()
4. onPostExecute()

在這些4種方法,只doInBackground()上後臺線程運行時,3和其他運行在主UI線程上。您應該在這3種方法中更新您的UI元素(即禁用Button),這取決於您的應用程序行爲。
您不允許更新doInBackground()上的UI。