OnProgressUpdate 无法在 TextView 中设置文本
OnProgressUpdate cant SetText in TextView
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());
}
此块在 Android Oreo 及更高版本上运行良好,但无法在以下任何版本上设置文本
日志猫:-
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.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.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.call(AsyncTask.java:292)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.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)
我假设你在 doInBackground 方法中直接调用这个方法导致了这个异常。
相反,尝试在调用 onProgressUpdate 方法
的 doInBackground 方法中调用 publishProgress({your values})
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());
}
此块在 Android Oreo 及更高版本上运行良好,但无法在以下任何版本上设置文本
日志猫:-
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.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.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.call(AsyncTask.java:292) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor.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)
我假设你在 doInBackground 方法中直接调用这个方法导致了这个异常。 相反,尝试在调用 onProgressUpdate 方法
的 doInBackground 方法中调用 publishProgress({your values})