问题
I have a fragment Fragment1.In that fragment I am parsing XML and populating list view.I have to execute the function parsing and population of list view in background of fragment.So that it will not disturb user.When I used Asynctask and defined population of list view function inside doInBackground it show an error like "an error occurred while executing doinbackground() ". What am I done wrong here?
Here is my Fragment
Fragment1=>
public class Kerala extends Fragment{
static String URL = "";
// XML node keys
static final String KEY_HEAD = "item"; // parent node
//static final String KEY_TITLE = "title";
static final String KEY_DATE = "pubDate";
public static String headflag="";
int f=0;
GridView list;
KeralaAdapter adapter;
public static String[] Title;
public static String[] Description;
public static String[] Tit;
public static String[] Tit2;
public static String[] Desc;
public static String[] Desc2;
public static String[] image;
public static String[] Date;
public static String[] Flash;
public static String[] Fl;
public static String[] Fl2;
private TextView mMessageView;
private Button mClearButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflating layout
View v = inflater.inflate(R.layout.kerala_fragment, container, false);
// We obtain layout references
return v;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
new ProgressAsyncTask().execute();
}
public void populate_listview()
{
URL="http://www.abcd.com/en/rssfeeds/9/latest/rss.xml";
ArrayList<HashMap<String, String>> newsList = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML from URL
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_HEAD);
// looping through all song nodes <song>
NodeList itemLst = doc.getElementsByTagName("item");
String MarqueeStr="";
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
//map.put(KEY_DATE, parser.getValue(e, KEY_DATE));
newsList.add(map);
}
list=(GridView)getActivity().findViewById(R.id.grid2);
// Getting adapter by passing xml data ArrayList
adapter=new KeralaAdapter(getActivity(), newsList);
list.setAdapter(adapter);
}
public void StartProgress(){
new ProgressAsyncTask().execute();
}
public class ProgressAsyncTask extends
AsyncTask<Void, Integer, Void> {
int myProgress;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
myProgress = 0;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
@Override
protected Void doInBackground(Void... arg0) {
// TODO Auto-generated method stub
keralaparser.parse();//here I am calling both functions
populate_listview();
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
}
}
}
Here is my error log,
11-14 18:29:42.705: E/AndroidRuntime(10530): java.lang.RuntimeException: An error occured while executing doInBackground()
11-14 18:29:42.705: E/AndroidRuntime(10530): at android.os.AsyncTask$3.done(AsyncTask.java:278)
11-14 18:29:42.705: E/AndroidRuntime(10530): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
11-14 18:29:42.705: E/AndroidRuntime(10530): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-14 18:29:42.705: E/AndroidRuntime(10530): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
11-14 18:29:42.705: E/AndroidRuntime(10530): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-14 18:29:42.705: E/AndroidRuntime(10530): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-14 18:29:42.705: E/AndroidRuntime(10530): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-14 18:29:42.705: E/AndroidRuntime(10530): at java.lang.Thread.run(Thread.java:856)
11-14 18:29:42.705: E/AndroidRuntime(10530): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
11-14 18:29:42.705: E/AndroidRuntime(10530): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4056)
11-14 18:29:42.705: E/AndroidRuntime(10530): at android.view.ViewRootImpl.focusableViewAvailable(ViewRootImpl.java:2210)
11-14 18:29:42.705: E/AndroidRuntime(10530): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:657)
11-14 18:29:42.705: E/AndroidRuntime(10530): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:657)
11-14 18:29:42.705: E/AndroidRuntime(10530): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:657)
11-14 18:29:42.705: E/AndroidRuntime(10530): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:657)
11-14 18:29:42.705: E/AndroidRuntime(10530): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:657)
11-14 18:29:42.705: E/AndroidRuntime(10530): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:657)
11-14 18:29:42.705: E/AndroidRuntime(10530): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:657)
11-14 18:29:42.705: E/AndroidRuntime(10530): at android.view.View.setFlags(View.java:6736)
11-14 18:29:42.705: E/AndroidRuntime(10530): at android.view.View.setFocusableInTouchMode(View.java:4714)
11-14 18:29:42.705: E/AndroidRuntime(10530): at android.widget.AdapterView.checkFocus(AdapterView.java:705)
11-14 18:29:42.705: E/AndroidRuntime(10530): at android.widget.GridView.setAdapter(GridView.java:186)
11-14 18:29:42.705: E/AndroidRuntime(10530): at com.madhyamam.malayalam2.Kerala.populate_listview(Kerala.java:149)
11-14 18:29:42.705: E/AndroidRuntime(10530): at com.madhyamam.malayalam2.Kerala$ProgressAsyncTask.doInBackground(Kerala.java:215)
11-14 18:29:42.705: E/AndroidRuntime(10530): at com.madhyamam.malayalam2.Kerala$ProgressAsyncTask.doInBackground(Kerala.java:1)
11-14 18:29:42.705: E/AndroidRuntime(10530): at android.os.AsyncTask$2.call(AsyncTask.java:264)
11-14 18:29:42.705: E/AndroidRuntime(10530): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
回答1:
You are doing changes to the ui in your async thread. Move the last 3 lines where you asign a new adapter to the list from your populate_listview() methode to onPostExecute(). This Method is executed on your ui thread and can change your ui.
回答2:
Call populate_listview();
in onPostExecute()
of your Asynctask
..
Log shows
Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
UI cannot be updated across threads directly.
回答3:
The idea of using an AsyncTask is to do background processes with the exception that no element of your current UI will be accessed / modified during the background process. This is a different thread. If you are trying to update / edit a list (ex: ListView / custom LinearLayout implemented like list views) of your current activity / fragment / UI, then you may consider generating in the background process the list [may be Plain Old Java Objects (POJOs) or may be views - new instances / inflated] and returning it as a result of the doInBackground() method:
@Override
protected View doInBackground(Void... params) {
return generateListResults();
}
After this, you may now access / modify your current activity / fragment / UI. This may be inside the onPostExecute() method, passing the result as a parameter, which may look like this, [following the doInBackground() method]:
@Override
protected void onPostExecute(View result) {
resultContainer.addView(result);
dialog.dismiss();
super.onPostExecute(result);
}
You may opt to show a loading dialog before starting the background process:
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(context, res.getString(R.string.searching), res.getString(R.string.wait), true);
resultContainer.removeAllViews();
super.onPreExecute();
}
来源:https://stackoverflow.com/questions/13379456/doinbackground-not-working-in-android-fragment