我正在尝试开发一个非常基本的应用程序,以便暂时使用 android 框架。我创建了一个包含 3 个 Activity 的简单应用程序。一种将数据插入数据库,一种处理确认消息,最后一种显示到目前为止输入的数据。
我从列表 Activity (最后执行的)中收到此错误
我不太清楚为什么会发生这种情况,但这就是 logcat 输出。我想做的就是在该 Activity 中执行以下操作:
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String responseStr = EntityUtils.toString(resEntity).trim();
Log.v(TAG, "Response: " + responseStr);
TextView responseSimpleText = new TextView(this);
responseSimpleText.setText(responseStr.toString());
// you can add an if statement here and do other actions based on the response
setContentView(responseSimpleText);
}else{
Log.v("ListingAcitivty.java", "Response is Null");
}
有人可以指出发生了什么吗?我正在使用 AsyncTask,并且使用它成功插入了数据。
最佳答案
从异常来看,您似乎正在尝试从与 UI 线程不同的线程更改 UI 上的某些内容。你不能直接这样做。
Because tasks that you run on a thread from a thread pool aren't running on your UI thread, they don't have access to UI objects.
来自:https://developer.android.com/training/multiple-threads/communicate-ui.html
该页面还解释了如何处理这种情况。
关于java - Android 应用程序显示线程错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17681506/