【发布时间】:2014-11-26 14:37:21
【问题描述】:
大家好,我从我的 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);
}
但他们都没有真正的帮助
我什至不知道从哪里开始寻找错误。也许你们中的某个人可以在这方面帮助我。
编辑1:
保存第一个条目后总是显示此错误。
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
编辑2:
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;
}
【问题讨论】:
-
您无法在
doInBackground上更改您的适配器。使用onPostExecute -
你能提供你的
doInBackground方法吗?
标签: android json android-listview android-asynctask