错误AsyncTask#1

时间:2014-11-26 14:37:21

标签: android json android-listview android-asynctask

大家好我从mysql数据库中获取此代码

[{"u_id":"8","id":"8","name":"test1","location_long":"10.01475","location_lati":"53.57823","description":"loo\nlol","date":"2014-11-21","time":"18:00:00","date_created":"2014-11-20","date_updated":"0000-00-00"},
{"u_id":"8","id":"9","name":"test9","location_long":"10.01475","location_lati":"53.57823","description":"test9\n","date":"2014-11-21","time":"19:00:00","date_created":"2014-11-20","date_updated":"0000-00-00"}, 
{"u_id":"8","id":"10","name":"test10","location_long":"10.01475","location_lati":"53.57823","description":"lol","date":"2014-11-22","time":"19:00:00","date_created":"2014-11-20","date_updated":"0000-00-00"},
{"u_id":"8","id":"11","name":"lol","location_long":"10.01475","location_lati":"53.57823","description":"","date":"2014-11-24","time":"18:00:00","date_created":"2014-11-20","date_updated":"0000-00-00"}]

我能够将它放入我的Android应用程序中。

我将数据插入到我的列表视图中。

for(int i = 0; i < jsonArray.length();i++)
            {
                Log.w("jsonArray:USER_ID", jsonArray.getJSONObject(i).getString("u_id"));
                Log.w("user_id:USER_ID", user_id);

                if(jsonArray.getJSONObject(i).getString("u_id")==user_id)
                    own_event = true;
                else
                    own_event = false;

                Log.w("jsonArray", jsonArray.getJSONObject(i).toString());

                adapter.add(new DataProvider(own_event,
                        jsonArray.getJSONObject(i).getString("description"),
                        jsonArray.getJSONObject(i).getString("name"),
                        jsonArray.getJSONObject(i).getString("date"),
                        jsonArray.getJSONObject(i).getString("time"),
                        jsonArray.getJSONObject(i).getDouble("location_long"),
                        jsonArray.getJSONObject(i).getDouble("location_lati"),
                        jsonArray.getJSONObject(i).getInt("id")));
            }

现在,当我在手机上运行应用程序时,出现此错误。

  

致命异常:AsyncTask#1       处理:com.myup2.up2,PID:4240       java.lang.RuntimeException:执行doInBackground()时发生错误   引起:android.view.ViewRootImpl $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触及其视图。

但有时我甚至不会收到错误并且应用程序正在运行并显示结果。

11-26 15:31:27.892    6936-6966/com.myup2.up2 W/params﹕ [user_id=8]
11-26 15:31:29.382    6936-6966/com.myup2.up2 W/jsoninsideParser﹕ [{"u_id":"8","id":"8","eventname":"test1","location_long":"10.01475","location_lati":"53.57823","description":"loo\nlol","date":"2014-11-21","time":"18:00:00","date_created":"2014-11-20","date_updated":"0000-00-00"},{"u_id":"8","id":"9","eventname":"test9","location_long":"10.01475","location_lati":"53.57823","description":"test9\n","date":"2014-11-21","time":"19:00:00","date_created":"2014-11-20","date_updated":"0000-00-00"},{"u_id":"8","id":"10","eventname":"test10","location_long":"10.01475","location_lati":"53.57823","description":"lol","date":"2014-11-22","time":"19:00:00","date_created":"2014-11-20","date_updated":"0000-00-00"},{"u_id":"8","id":"11","eventname":"lol","location_long":"10.01475","location_lati":"53.57823","description":"","date":"2014-11-24","time":"18:00:00","date_created":"2014-11-20","date_updated":"0000-00-00"}]
11-26 15:31:29.402    6936-6966/com.myup2.up2 W/jsonArray:USER_ID﹕ 8
11-26 15:31:29.402    6936-6966/com.myup2.up2 W/user_id:USER_ID﹕ 8
11-26 15:31:29.412    6936-6966/com.myup2.up2 W/jsonArray﹕ {"id":"8","time":"18:00:00","location_lati":"53.57823","description":"loo\nlol","date_updated":"0000-00-00","date_created":"2014-11-20","date":"2014-11-21","u_id":"8","location_long":"10.01475","eventname":"test1"}
11-26 15:31:29.412    6936-6966/com.myup2.up2 W/dataprovider_item_created﹕ test1
11-26 15:31:29.412    6936-6966/com.myup2.up2 W/dataprovider_item_own_event﹕ false
11-26 15:31:29.412    6936-6966/com.myup2.up2 W/jsonArray:USER_ID﹕ 8
11-26 15:31:29.412    6936-6966/com.myup2.up2 W/user_id:USER_ID﹕ 8
11-26 15:31:29.422    6936-6966/com.myup2.up2 W/jsonArray﹕ {"id":"9","time":"19:00:00","location_lati":"53.57823","description":"test9\n","date_updated":"0000-00-00","date_created":"2014-11-20","date":"2014-11-21","u_id":"8","location_long":"10.01475","eventname":"test9"}
11-26 15:31:29.422    6936-6966/com.myup2.up2 W/dataprovider_item_created﹕ test9
11-26 15:31:29.422    6936-6966/com.myup2.up2 W/dataprovider_item_own_event﹕ false
11-26 15:31:29.422    6936-6966/com.myup2.up2 W/jsonArray:USER_ID﹕ 8
11-26 15:31:29.422    6936-6966/com.myup2.up2 W/user_id:USER_ID﹕ 8
11-26 15:31:29.432    6936-6966/com.myup2.up2 W/jsonArray﹕ {"id":"10","time":"19:00:00","location_lati":"53.57823","description":"lol","date_updated":"0000-00-00","date_created":"2014-11-20","date":"2014-11-22","u_id":"8","location_long":"10.01475","eventname":"test10"}
11-26 15:31:29.442    6936-6966/com.myup2.up2 W/dataprovider_item_created﹕ test10
11-26 15:31:29.442    6936-6966/com.myup2.up2 W/dataprovider_item_own_event﹕ false
11-26 15:31:29.442    6936-6966/com.myup2.up2 W/jsonArray:USER_ID﹕ 8
11-26 15:31:29.442    6936-6966/com.myup2.up2 W/user_id:USER_ID﹕ 8
11-26 15:31:29.452    6936-6966/com.myup2.up2 W/jsonArray﹕ {"id":"11","time":"18:00:00","location_lati":"53.57823","description":"","date_updated":"0000-00-00","date_created":"2014-11-20","date":"2014-11-24","u_id":"8","location_long":"10.01475","eventname":"lol"}
11-26 15:31:29.452    6936-6966/com.myup2.up2 W/dataprovider_item_created﹕ lol
11-26 15:31:29.452    6936-6966/com.myup2.up2 W/dataprovider_item_own_event﹕ false

当出现错误时,它会向我显示不同的地方:

adapter.registerDataSetObserver(new DataSetObserver(){
            @Override
            public void onChanged() {
                super.onChanged();
                listview.setSelection(adapter.getCount()-1);
            }
        });

@Override
    public void add(DataProvider object) {
        event_list.add(object);
        super.add(object);
    }

但他们都没有真正帮助

我甚至不知道从哪里开始搜索错误。也许有人可以帮助我。

EDIT1:

保存第一个条目后,始终显示此错误。

11-26 15:40:34.702    8062-8093/com.myup2.up2 W/jsonArray﹕{"id":"8","time":"18:00:00","location_lati":"53.57823","description":"loo\nlol","date_updated":"0000-00-00","date_created":"2014-11-20","date":"2014-11-21","u_id":"8","location_long":"10.01475","eventname":"test1"}
11-26 15:40:34.702    8062-8093/com.myup2.up2 W/dataprovider_item_created﹕ test1
11-26 15:40:34.702    8062-8093/com.myup2.up2 W/dataprovider_item_own_event﹕ false
11-26 15:40:34.702    8062-8093/com.myup2.up2 W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0x41771ce0)
11-26 15:40:34.922    8062-8093/com.myup2.up2 E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1

EDIT2:

protected String doInBackground(String... args) {
            // TODO Auto-generated method stub
            // Check for success tag
            int success;

            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Home.this);
            String user_id = sp.getString("user_id", "anon");

            try {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("user_id", user_id));

                Log.w("params", params.toString());

                Log.d("request!", "starting");
                // getting product details by making HTTP request
                JSONArray jsonArray = jsonParser.makeHttpRequest(
                        EVENTS_URL, "POST", params);



                for(int i = 0; i < jsonArray.length();i++)
                {
                    Log.w("jsonArray:USER_ID", jsonArray.getJSONObject(i).getString("u_id"));
                    Log.w("user_id:USER_ID", user_id);

                    if(jsonArray.getJSONObject(i).getString("u_id")==user_id)
                        own_event = true;
                    else
                        own_event = false;

                    Log.w("jsonArray", jsonArray.getJSONObject(i).toString());

                    adapter.add(new DataProvider(own_event,
                            jsonArray.getJSONObject(i).getString("description"),
                            jsonArray.getJSONObject(i).getString("eventname"),
                            jsonArray.getJSONObject(i).getString("date"),
                            jsonArray.getJSONObject(i).getString("time"),
                            jsonArray.getJSONObject(i).getDouble("location_long"),
                            jsonArray.getJSONObject(i).getDouble("location_lati"),
                            jsonArray.getJSONObject(i).getInt("id")));
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;

        }

4 个答案:

答案 0 :(得分:1)

您必须将方法移动到UI线程。在您的情况onPostExecute中,您无法在后台线程中修改视图等。

protected ArrayList<DataProvider> doInBackground(String... args) {
            // TODO Auto-generated method stub
            // Check for success tag
            int success;

            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Home.this);
            String user_id = sp.getString("user_id", "anon");

            ArrayList<DataProvider> dataProviders = new ArrayList<DataProvider>();

            try {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("user_id", user_id));

                Log.w("params", params.toString());

                Log.d("request!", "starting");
                // getting product details by making HTTP request
                JSONArray jsonArray = jsonParser.makeHttpRequest(
                        EVENTS_URL, "POST", params);



                for(int i = 0; i < jsonArray.length();i++)
                {
                    Log.w("jsonArray:USER_ID", jsonArray.getJSONObject(i).getString("u_id"));
                    Log.w("user_id:USER_ID", user_id);

                    if(jsonArray.getJSONObject(i).getString("u_id")==user_id)
                        own_event = true;
                    else
                        own_event = false;

                    Log.w("jsonArray", jsonArray.getJSONObject(i).toString());

                    dataProviders.add(new DataProvider(own_event,
                            jsonArray.getJSONObject(i).getString("description"),
                            jsonArray.getJSONObject(i).getString("eventname"),
                            jsonArray.getJSONObject(i).getString("date"),
                            jsonArray.getJSONObject(i).getString("time"),
                            jsonArray.getJSONObject(i).getDouble("location_long"),
                            jsonArray.getJSONObject(i).getDouble("location_lati"),
                            jsonArray.getJSONObject(i).getInt("id")));
                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

            return dataProviders;

        }


    protected void onPostExecute(ArrayList<DataProvider> list)
{
    for(int i = 0; i < list.size(); i++){
        adapter.add(list.get(i));
    }
} 

答案 1 :(得分:0)

问题是由ListView方法操纵doInBackground()造成的。您需要将操作移动到onPostExecute()方法中。如果要在DataSetObserver()方法中创建doInBackground(),请小心,因为它可能绑定到AsyncTask而不是UI线程使用的后台线程。

答案 2 :(得分:0)

您不能也不应该修改非UI线程的线程中的UI。

喜欢它:

protected JSONArray doInBackground(String... args)
{
    // Check for success tag
    int success;

    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Home.this);
    String user_id = sp.getString("user_id", "anon");

    try {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("user_id", user_id));

        Log.w("params", params.toString());

        Log.d("request!", "starting");
        // getting product details by making HTTP request
        return jsonParser.makeHttpRequest(EVENTS_URL, "POST", params);    
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

    return null;
}

然后:

protected void onPostExecute(JSONArray jsonArray)
{
    for(int i = 0; i < jsonArray.length();i++)
    {
        Log.w("jsonArray:USER_ID", jsonArray.getJSONObject(i).getString("u_id"));
        Log.w("user_id:USER_ID", user_id);
        if(jsonArray.getJSONObject(i).getString("u_id")==user_id)
            own_event = true;
        else
            own_event = false;

        Log.w("jsonArray", jsonArray.getJSONObject(i).toString());
        adapter.add(new DataProvider(own_event,
            jsonArray.getJSONObject(i).getString("description"),
            jsonArray.getJSONObject(i).getString("eventname"),
            jsonArray.getJSONObject(i).getString("date"),
            jsonArray.getJSONObject(i).getString("time"),
            jsonArray.getJSONObject(i).getDouble("location_long"),
            jsonArray.getJSONObject(i).getDouble("location_lati"),
            jsonArray.getJSONObject(i).getInt("id")));
    }
    // Other stuff already in onPostExecute
} 

答案 3 :(得分:0)

在UI上更新内容时,您必须在UI线程上更新它们,这是Async中的OnPostExecute。实施例。

public class UpdateUIThread extends AsyncTask<Void,Void,Void>{
    JSONArray array;
    @Override
    protected Void doInBackground(Void... params) {
    //Do Network Stuff Here
    //array = (Insert String Here)
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        for(int i = 0; i < jsonArray.length();i++){
        JSONObject jObj  = array.getJSONObject(i);
      String string1 = jObj.getString("Name Of Object");
         adapter.add(new YourClass(string1));
        }
    }
}