【发布时间】:2014-01-09 07:32:37
【问题描述】:
我正在开发一个 Android 应用程序,我使用 Eclipse 作为 IDE,我的数据库是 MySQL。我有以下问题。当我尝试通过 AsynTask 从数据库中获取数据时,出现如下所示的错误。
android.view.ViewRootImpl$CalledFromWrongThreadException:
Only the original thread that created a view hierarchy
can touch its views
活动:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
asyn=new MyAsyncTask();
tvtexto=(TextView)findViewById(R.id.tvtextoMa);
context = getApplicationContext();
eventos = new ArrayList<Eventos>();
eventosAdapter = new EventosAdapter(context, R.layout.filae, eventos);
eventos=new ArrayList<Eventos>();
asyn.execute();
listView = (ListView)findViewById(R.id.ListView01Ma);
}
异步任务:
public class MyAsyncTask extends AsyncTask<Void, Void, Void>
{
@Override
protected Void doInBackground(Void... arg0)
{
eventos=(ArrayList<Eventos>) BDEventos.getDatosEventos();
for(Eventos n:eventos){
eventosAdapter.add(n);
}
eventosAdapter.notifyDataSetChanged();
listView.setAdapter(eventosAdapter);
return null;
}
}
【问题讨论】:
-
你会发现很多搜索“[android] CalledFromWrongThreadException”的问题的答案。