有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java 安卓应用程序显示线程错误

我正在尝试开发一个非常基本的应用程序,以便目前使用安卓框架。我创建了一个包含3个活动的简单应用程序。一个将数据插入数据库,一个处理确认消息,最后一个显示迄今为止输入的数据

我从列表活动(最后执行的)中收到这个错误Check picture to see error

我不太清楚为什么会发生这种情况,但这是logcat的输出。在这项活动中,我只想做以下几点:

 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,并且使用它成功地插入了数据


共 (1) 个答案

  1. # 1 楼答案

    从异常情况来看,您似乎试图从与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

    该页面还解释了应该如何处理这种情况