Issue
protected Long doInBackground(String... address) {
while(true){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
setProgress(100);
onProgressUpdate(getping(address[0]));
}
}
protected void onProgressUpdate(Long... values){
result.setText(String.valueOf(values[0]));
builder.setContentText(String.valueOf(values[0]));
notificationManager.notify(1,builder.build());
}
This chunk works fine on Android Oreo and above but fails to set text on any version below
LOG CAT:-
11-05 16:18:26.041 4720-4752/com.pcartistofficial.pingtesterforgames E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 Process: com.pcartistofficial.pingtesterforgames, PID: 4720 java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask$3.done(AsyncTask.java:304) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) at java.util.concurrent.FutureTask.setException(FutureTask.java:222) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 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:6357) at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:909) at android.view.ViewGroup.invalidateChild(ViewGroup.java:4690) at android.view.View.invalidateInternal(View.java:11801) at android.view.View.invalidate(View.java:11765) at android.view.View.invalidate(View.java:11749) at android.widget.TextView.checkForRelayout(TextView.java:6858) at android.widget.TextView.setText(TextView.java:4057) at android.widget.TextView.setText(TextView.java:3915) at android.widget.TextView.setText(TextView.java:3890) at com.pcartistofficial.pingtesterforgames.MainActivity$BackgroundTasker.onProgressUpdate(MainActivity.java:96) at com.pcartistofficial.pingtesterforgames.MainActivity$BackgroundTasker.doInBackground(MainActivity.java:90) at com.pcartistofficial.pingtesterforgames.MainActivity$BackgroundTasker.doInBackground(MainActivity.java:67) at android.os.AsyncTask$2.call(AsyncTask.java:292) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818)
Solution
I assume that you call this method directly in doInBackground method cause this exception. Instead, try to call publishProgress({your values}) in doInBackground method which calls onProgressUpdate method
Reference to AsyncTask API documentation
Answered By - Royi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.