Reputation: 1559
I tried to load listview data by using asynctask. when the first activity call to start new activity, it should load new activity and display the data. but my second activity not started.
My second class is
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.search_data);
search_data=getIntent().getExtras().getString("Search_Value");
list = (ListView) findViewById(R.id.listView1);
accessWebService();
}
class SearchData extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("rest_name", search_data));
Log.d("request!", "starting");
JSONObject json = jsonParser.makeHttpRequest(Search, "POST", params);
Log.d("Search", json.toString());
JSONArray jsonMainNode = json.optJSONArray("Search");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode;
try {
jsonChildNode = jsonMainNode.getJSONObject(i);
String restName = jsonChildNode.optString("name");
//Toast.makeText(getApplicationContext(), name, Toast.LENGTH_LONG).show();
String address = jsonChildNode.optString("address");
String mobile = jsonChildNode.optString("mobile");
String direction = "Direction: "+jsonChildNode.optString("direction");
String bestTime = "Best time to visite: "+jsonChildNode.optString("bestTime");
String food = jsonChildNode.optString("food");
String dress = jsonChildNode.optString("dress");
String priceRange = "Price Range: "+jsonChildNode.optString("priceRange");
Log.d("Dress", dress);
listData = new ArrayList<String>();
listData.add(restName);
listData.add(address);
listData.add(mobile);
listData.add(direction);
listData.add(bestTime);
listData.add(food);
listData.add(dress);
listData.add(priceRange);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
MyListAdapter adapter = new MyListAdapter(Search.this,listData);
list.setAdapter(adapter);
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
}
public void accessWebService() {
SearchData data = new SearchData();
// passes values for the urls string array
data.execute();
}
when i run it, following errors were occurred.
01-16 15:19:48.092: E/AndroidRuntime(10724): FATAL EXCEPTION: AsyncTask #1
01-16 15:19:48.092: E/AndroidRuntime(10724): java.lang.RuntimeException: An error occured while executing doInBackground()
01-16 15:19:48.092: E/AndroidRuntime(10724): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-16 15:19:48.092: E/AndroidRuntime(10724): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-16 15:19:48.092: E/AndroidRuntime(10724): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-16 15:19:48.092: E/AndroidRuntime(10724): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-16 15:19:48.092: E/AndroidRuntime(10724): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-16 15:19:48.092: E/AndroidRuntime(10724): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-16 15:19:48.092: E/AndroidRuntime(10724): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-16 15:19:48.092: E/AndroidRuntime(10724): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-16 15:19:48.092: E/AndroidRuntime(10724): at java.lang.Thread.run(Thread.java:856)
01-16 15:19:48.092: E/AndroidRuntime(10724): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-16 15:19:48.092: E/AndroidRuntime(10724): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:5073)
01-16 15:19:48.092: E/AndroidRuntime(10724): at android.view.ViewRootImpl.focusableViewAvailable(ViewRootImpl.java:2809)
01-16 15:19:48.092: E/AndroidRuntime(10724): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:609)
01-16 15:19:48.092: E/AndroidRuntime(10724): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:609)
01-16 15:19:48.092: E/AndroidRuntime(10724): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:609)
01-16 15:19:48.092: E/AndroidRuntime(10724): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:609)
01-16 15:19:48.092: E/AndroidRuntime(10724): at android.view.View.setFlags(View.java:8414)
01-16 15:19:48.092: E/AndroidRuntime(10724): at android.view.View.setFocusableInTouchMode(View.java:5693)
01-16 15:19:48.092: E/AndroidRuntime(10724): at android.widget.AdapterView.checkFocus(AdapterView.java:720)
01-16 15:19:48.092: E/AndroidRuntime(10724): at android.widget.ListView.setAdapter(ListView.java:467)
01-16 15:19:48.092: E/AndroidRuntime(10724): at com.example.rp.Search$SearchData.doInBackground(Search.java:102)
01-16 15:19:48.092: E/AndroidRuntime(10724): at com.example.rp.Search$SearchData.doInBackground(Search.java:1)
01-16 15:19:48.092: E/AndroidRuntime(10724): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-16 15:19:48.092: E/AndroidRuntime(10724): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-16 15:19:48.092: E/AndroidRuntime(10724): ... 5 more
Can anyone help me to solve this problem?
Upvotes: 0
Views: 76
Reputation: 3155
it seems there is a problem with MyListAdapter
MyListAdapter adapter = new MyListAdapter(Search.this,listData);
list.setAdapter(adapter);
You need to call runOnUiThread() from onPostExecute.
runOnUiThread(new Runnable() {
@Override
public void run() {
MyListAdapter adapter = new MyListAdapter(Search.this,listData);
list.setAdapter(adapter);
}
});
Upvotes: 0
Reputation: 18933
Move this to onPostExecute()
MyListAdapter adapter = new MyListAdapter(Search.this,listData);
list.setAdapter(adapter);
This is because you can't update your UI in doInbackGround method.
For update your UI you have to update it in onPostExecute method.
In doInbackGround you can perform your Network UI work.
Upvotes: 1
Reputation: 2664
Move these lines to onPostExecute
because, doInBackGround
will run in Non-UI Thread
MyListAdapter adapter = new MyListAdapter(Search.this,listData);
list.setAdapter(adapter);
Upvotes: 2