Reputation: 311
Hi everyone I get this code from my mysql database
[{"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"}]
I was able to get it into my android application.
I insert the data into my listview.
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")));
}
Now when I run the application on my phone, I get this error.
FATAL EXCEPTION: AsyncTask #1 Process: com.myup2.up2, PID: 4240 java.lang.RuntimeException: An error occured while executing doInBackground() Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
But sometimes i dont even get an error and the application is running and showing the results.
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
When the error appears it shows me thses different places:
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);
}
But none of them really help
I dont even know where to start searching for the error. Maybe someone of you can help me on that.
Edit1:
It is always showing this error after the first entry is saved.
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;
}
Upvotes: 0
Views: 49
Reputation: 16833
You can't and you should not modify the UI in a thread that is not the UI thread.
Do it like it :
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;
}
And then :
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
}
Upvotes: 0
Reputation: 22064
You will have to move your method to the UI Thread. Which will be in your case onPostExecute
, you can not modify views and such in a Background Thread.
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));
}
}
Upvotes: 1
Reputation: 167
When updating things on the UI you have to update them on the UI Thread which is OnPostExecute in Async. Example.
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));
}
}
}
Upvotes: 0
Reputation: 15775
The problem is being caused by manipulating your ListView
from within your doInBackground()
method. You'll need to move the manipulation into the onPostExecute()
method. Be careful if you are creating the DataSetObserver()
in your doInBackground()
method as it is likely being bound to the background thread used by AsyncTask
rather than the UI thread.
Upvotes: 0