doInBackground不是Android的碎片工作

doInBackground不是Android的碎片工作

本文介绍了doInBackground不是Android的碎片工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段是Fragment1.In片段我解析XML和填充列表view.I已执行函数解析和人口fragment.So背景列表视图,它不会打扰user.When我用的AsyncTask和在执行doinbackground()发生了错误内部doInBackground它表明类似的错误列表视图功能的特定人群。我究竟错在这里做什么?

下面是我的片段

片段1 =>

 公共类喀拉拉邦扩展片段{
   静态字符串URL =;
// XML节点键
静态最后弦乐KEY_HEAD =项; //父节点
//静态最后弦乐KEY_TITLE =称号;
静态最后弦乐KEY_DATE =pubdate的;
公共静态字符串headflag =;
INT F = 0;
GridView控件列表;
    KeralaAdapter适配器;
  公共静态的String []标题;
    公共静态的String []说明;
    公共静态的String []山雀;
    公共静态的String [] TIT2;
    公共静态的String []说明;
    公共静态的String [] DESC2;    公共静态的String []的形象;
    公共静态的String []日期;
    公共静态的String []闪光;
    公共静态的String []佛罗里达州;
    公共静态的String [] FL2;私人TextView的mMessageView;
私人按钮mClearButton;@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
        捆绑savedInstanceState){
    //充气布局
    视图V = inflater.inflate(R.layout.kerala_fragment,集装箱,FALSE);
    //我们得到的布局引用    返回伏;}@覆盖
公共无效onViewCreated(查看视图,捆绑savedInstanceState){
    super.onViewCreated(查看,savedInstanceState);
    新ProgressAsyncTask()执行();
}
 公共无效populate_listview()
 {
     URL =htt​​p://www.abcd.com/en/rssfeeds/9/latest/rss.xml;    ArrayList的<&HashMap的LT;字符串,字符串>>新闻列表=新的ArrayList<&HashMap的LT;字符串,字符串>>();    XMLParser的解析器=新XMLParser的();
    字符串XML = parser.getXmlFromUrl(URL); // URL从XML获得
    文档的文档= parser.getDomElement(XML); //获得DOM元素    节点列表NL = doc.getElementsByTagName(KEY_HEAD);
    //遍历所有歌曲节点&LT循环;宋>
    节点列表itemLst = doc.getElementsByTagName(项目);
    串MarqueeStr =;    的for(int i = 0; I< nl.getLength();我++){
        //创建新的HashMap
        HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
        元素e =(元)nl.item(I)        //map.put(KEY_DATE,parser.getValue(即KEY_DATE));        newsList.add(地图);
 }    名单=(GridView控件)getActivity()findViewById(R.id.grid2)。    //通过传递XML数据的ArrayList获取适配器
            适配器=新KeralaAdapter(getActivity(),新闻列表);
            list.setAdapter(适配器);}
 公共无效StartProgress(){
        新ProgressAsyncTask()执行();
    }    公共类ProgressAsyncTask扩展
        AsyncTask的<无效,整型,太虚> {        INT myProgress;        @覆盖
        在preExecute保护无效(){
            // TODO自动生成方法存根
            super.on preExecute();            myProgress = 0;
        }        @覆盖
        保护无效onPostExecute(虚空结果){
            // TODO自动生成方法存根
            super.onPostExecute(结果);        }        @覆盖
        保护无效doInBackground(虚空......为arg0){
            // TODO自动生成方法存根
                keralaparser.parse(); //这里我打电话两种功能
            populate_listview();
            返回null;
        }        @覆盖
        保护无效onProgressUpdate(整数...值){
            // TODO自动生成方法存根        }        }
}

这是我的错误日志,

  11-14 18:29:42.705:E / AndroidRuntime(10530):了java.lang.RuntimeException:执行doInBackground发生错误()
11-14 18:29:42.705:E / AndroidRuntime(10530):在android.os.AsyncTask $ 3.done(AsyncTask.java:278)
11-14 18:29:42.705:E / AndroidRuntime(10530):在java.util.concurrent.FutureTask中$ Sync.innerSetException(FutureTask.java:273)
11-14 18:29:42.705:E / AndroidRuntime(10530):在java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-14 18:29:42.705:E / AndroidRuntime(10530):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:307)
11-14 18:29:42.705:E / AndroidRuntime(10530):在java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-14 18:29:42.705:E / AndroidRuntime(10530):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-14 18:29:42.705:E / AndroidRuntime(10530):在java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:569)
11-14 18:29:42.705:E / AndroidRuntime(10530):在java.lang.Thread.run(Thread.java:856)
11-14 18:29:42.705:E / AndroidRuntime(10530):$ android.view.ViewRootImpl CalledFromWrongThreadException:只有创建视图层次可以触摸其观点原来的线程所致。
11-14 18:29:42.705:E / AndroidRuntime(10530):在android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4056)
11-14 18:29:42.705:E / AndroidRuntime(10530):在android.view.ViewRootImpl.focusableViewAvailable(ViewRootImpl.java:2210)
11-14 18:29:42.705:E / AndroidRuntime(10530):在android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:657)
11-14 18:29:42.705:E / AndroidRuntime(10530):在android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:657)
11-14 18:29:42.705:E / AndroidRuntime(10530):在android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:657)
11-14 18:29:42.705:E / AndroidRuntime(10530):在android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:657)
11-14 18:29:42.705:E / AndroidRuntime(10530):在android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:657)
11-14 18:29:42.705:E / AndroidRuntime(10530):在android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:657)
11-14 18:29:42.705:E / AndroidRuntime(10530):在android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:657)
11-14 18:29:42.705:E / AndroidRuntime(10530):在android.view.View.setFlags(View.java:6736)
11-14 18:29:42.705:E / AndroidRuntime(10530):在android.view.View.setFocusableInTouchMode(View.java:4714)
11-14 18:29:42.705:E / AndroidRuntime(10530):在android.widget.AdapterView.checkFocus(AdapterView.java:705)
11-14 18:29:42.705:E / AndroidRuntime(10530):在android.widget.GridView.setAdapter(GridView.java:186)
11-14 18:29:42.705:E / AndroidRuntime(10530):在com.madhyamam.malayalam2.Kerala.populate_listview(Kerala.java:149)
11-14 18:29:42.705:E / AndroidRuntime(10530):在com.madhyamam.malayalam2.Kerala $ ProgressAsyncTask.doInBackground(Kerala.java:215)
11-14 18:29:42.705:E / AndroidRuntime(10530):在com.madhyamam.malayalam2.Kerala $ ProgressAsyncTask.doInBackground(Kerala.java:1)
11-14 18:29:42.705:E / AndroidRuntime(10530):在android.os.AsyncTask $ 2.call(AsyncTask.java:264)
11-14 18:29:42.705:E / AndroidRuntime(10530):在java.util.concurrent.FutureTask中$ Sync.innerRun(FutureTask.java:305)


解决方案

您正在做的更改UI在异步线程。移动过去的3条线,你ASIGN一个新的适配器列表从populate_listview()梅索德到onPostExecute()。在你的UI线程执行这个方法,可以改变你的UI。

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)
解决方案

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.

这篇关于doInBackground不是Android的碎片工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:10