I get crash when i’m trying to run function doTask() in the background. I was trying with new Thread(new Runnable() {}) and it works only this section:
handlerForBar.post(new Runnable() {public void run() { doTask(); } })
but the progressBar appears when the work of doTask() is finished. So I thought that AsyncTask could work, but it crashes.
public void doTask()
{
ListView listView = (ListView)findViewById(R.id.list);
myArray = new ArrayList<HashMap<String,Object>>();
HashMap<String, Object> hash_map;
hash_map = new HashMap<String, Object>();
hash_map.put(NICK_KEY, "nick");
myArray.add(hash_map);
listView.setAdapter(new myListAdapter(myArray,this));
new myListAdapter(myArray,this).notifyDataSetChanged();
}
private class myThread extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
doTask(); //try, catch also FC
return null;
}
...
}
Structure of *.java:
> public class mainActivity extends Activity{}
> public void onCreate()
> new myThread().execute("");
> public void doTask()
> private class myThread extends AsyncTask<String, Void, String>{}
> protected String doInBackground()
> doTask()
> private class myListView extends BaseAdapter
You can’t touch the UI in
doInBackground, you have to update the UI in methods likeonProgressUpdateoronPostExecute. See here