使用Async Task和进程栏

时间:2012-03-17 09:48:06

标签: android progress-bar android-asynctask

我写了一些应用程序,使用lauot-procce bar和AsyncTask对xml进行一些工作并将其显示给用户。 我想在后退过程结束后对进程栏进行破坏 但我的代码有一些问题

错误:

FATAL EXCEPTION: AsyncTask #2
 03-17 11:25:29.559: E/AndroidRuntime(31114): java.lang.RuntimeException: An error           while executing doInBackground()
  03-17 11:25:29.559: E/AndroidRuntime(31114):  at          android.os.AsyncTask$3.done(AsyncTask.java:200)
  03-17 11:25:29.559: E/AndroidRuntime(31114):  at   java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
  03-17 11:25:29.559: E/AndroidRuntime(31114):  at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
  03-17 11:25:29.559: E/AndroidRuntime(31114):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
 03-17 11:25:29.559: E/AndroidRuntime(31114):   at java.util.concurrent.FutureTask.run(FutureTask.java:138)
 03-17 11:25:29.559: E/AndroidRuntime(31114):   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
 03-17 11:25:29.559: E/AndroidRuntime(31114):   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
 03-17 11:25:29.559: E/AndroidRuntime(31114):   at java.lang.Thread.run(Thread.java:1019)
03-17 11:25:29.559: E/AndroidRuntime(31114): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
03-17 11:25:29.559: E/AndroidRuntime(31114):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
0    3-17 11:25:29.559: E/AndroidRuntime(31114):    at java.util.ArrayList.get(ArrayList.java:311)
   03-17 11:25:29.559: E/AndroidRuntime(31114):     at com.example.News.NewsActivity$GetDataTask.doInBackground(NewsActivity.java:67)
  03-17 11:25:29.559: E/AndroidRuntime(31114):  at 

我的代码是:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.progdailog);
mContext = this;
    new GetDataTask().execute();
}
 protected void onPostExecute(Integer result) {

        listAdapter = new CustomListAdapter(mContext, R.layout.list_item,
                ListNews);
        ListView lv = ((ListActivity) mContext).getListView();
        lv.setAdapter(listAdapter); // on test

        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                if (position != 0) {

                    // When clicked, show a toast with the TextView text
                    String s = ReadXML.hadashotListItems.get(position - 1).link;

                    Intent intent = new Intent(NewsActivity.this,
                            WebActivity.class);
                    intent.putExtra("url", s);

                    startActivity(intent);

                }
            }

            public void onScrollStateChanged(final AbsListView view,
                    final int scrollState) {
                if (scrollState == 0)
                    listAdapter.notifyDataSetChanged();
            }
        });

        //progDailog.dismiss();
        super.onPostExecute(result);

    }

}

第67行是代码:

 protected Integer doInBackground(Void... params) {
        try {
            ReadXML = new ReadXMLFile(mContext, getIntent().getExtras()
                    .getString("urlXml"));

            ListNews.add("NEWS");
            if (checkDate(ReadXML.hadashotListItems.get(0).pubDate,ReadXML.hadashotListItems.get(1).pubDate))
            for (int i = 0; i < ReadXML.hadashotListItems.size(); i++) {

                ListNews.add(ReadXML.hadashotListItems.get(i).title + "\n"
                        + ReadXML.hadashotListItems.get(i).pubDate);
            }
            else
                for (int i = ReadXML.hadashotListItems.size()-1; i >=0; i--) {

                    ListNews.add(ReadXML.hadashotListItems.get(i).title + "\n"
                            +     ReadXML.hadashotListItems.get(i).pubDate);
                }
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (java.text.ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return 1;
            }  

我认为观点有问题。您可以帮助更改它将起作用的代码。

1 个答案:

答案 0 :(得分:1)

NewsActivity.java第67行有一个IndexOutOfBoundsException。

IndexOutOfBoundsException: Invalid index 0, size is 0

当没有元素时,您正尝试访问索引0。我认为您没有发布该代码,因此请查找该行并修复错误的索引访问。