本文介绍了只有创建一个视图层次可以触摸它views.onProgressUpdate原来的线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Android 4.0开始我有问题(在2.3.6这是工作)
onProgressUpdate
应该已在UI线程。
我曾尝试 activity.runOnUiThread
现与处理程序
版本,OFC首先是没有任何艾德里安的。如何解决呢?
公共类ReadDataTask扩展的AsyncTask<字符串,字符串,JSONArray> { 私人活动活动;
保护ProgressDialog DLG; 私人长期startRead,endRead,endJson; 公共ReadDataTask(活动活动){
this.activity =活动;
DLG =新ProgressDialog(活动);
} @覆盖
在preExecute保护无效(){
super.on preExecute();
dlg.show();
startRead = System.currentTimeMillis的();
} @覆盖
保护无效onProgressUpdate(字符串值...){
super.onProgressUpdate(值);
如果(值=空&放大器;!&放大器; values.length大于0){
最后弦乐味精=值[0];
新的处理程序()。后(新的Runnable(){
公共无效的run(){
dlg.setMessage(MSG); // android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次可以触摸其观点原来的线程。
}
});
}
}
@覆盖
保护JSONArray doInBackground(字符串... PARAMS){
......长在这里的任务
publishProgress(转换为JSON:+(1 + I)+/+ dataList.size());
解决方案
在preExecute创建处理程序只有在键()
这是从UI线程调用和保存在 onProgressUpdate对它的引用,比邮寄给它()
。
处理器帖子线程,它已经创建的。
Starting from Android 4.0 I have the problem ( on 2.3.6 it is working).
onProgressUpdate
it should be already in UI thread.I have tried activity.runOnUiThread
now is with a Handler
version, ofc first was without any of tham. How to fix it?
public class ReadDataTask extends AsyncTask<String, String, JSONArray> {
private Activity activity;
protected ProgressDialog dlg;
private long startRead, endRead, endJson;
public ReadDataTask(Activity activity) {
this.activity = activity;
dlg = new ProgressDialog(activity);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
dlg.show();
startRead = System.currentTimeMillis();
}
@Override
protected void onProgressUpdate(String... values) {
super.onProgressUpdate(values);
if (values != null && values.length > 0) {
final String msg = values[0];
new Handler().post(new Runnable() {
public void run() {
dlg.setMessage(msg);// android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
}
});
}
}
@Override
protected JSONArray doInBackground(String... params) {
... long task here
publishProgress("Converting to JSON: " + (1 + i) + " / " + dataList.size());
解决方案
Create handler only once in onPreExecute()
which is called from UI thread and save a reference to it, than post to it in onProgressUpdate()
.
Handler posts to thread that it has been created on.
这篇关于只有创建一个视图层次可以触摸它views.onProgressUpdate原来的线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!