【问题标题】:java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0java.lang.IndexOutOfBoundsException:索引 0 无效,大小为 0
【发布时间】:2012-09-05 13:53:38
【问题描述】:

我有一个大问题,我正在尝试从列表中的数据库获取数据 这是我的代码:

private class LoadData extends AsyncTask<Void, Void, Void> { 
    private ProgressDialog progressDialog;  
    @Override

    protected void onPreExecute() {

    CharSequence contentTitle = getString(R.string.loading);
    this.progressDialog = ProgressDialog.show(TabFreeBooksActivity.this,"",contentTitle);   
    }
    @Override
    protected void onPostExecute(final Void unused) {  
        this.progressDialog.dismiss();  
        showoffreebooks();

    }

    @Override
    protected Void doInBackground(Void... params) {
        // TODO Auto-generated method stub  
        // HTTP post
        sharedPreference3 = getSharedPreferences(downnplace, 0);
        downoptionplace = sharedPreference3.getString("downloadflagplace","internal");
        Cursor bokgids = ggp.gids();
        do{

            allgids.add(bokgids.getString(0));

        }
        while(bokgids.moveToNext());
        for(int o=0;o<allgids.size();o++)
        {

        ggp.unupdatduplicate(allgids.get(o));   
        System.out.println("allgids2: "+allgids.get(o));
        System.out.println("ggg: "+ggp.unupdatduplicate(allgids.get(o)));
        }

        getoffrecentbooks();        


        return null;

    }

}

AsyncTask 的调用是:

 public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.demo);
    frboks= new DAOfreeboks(this);
    ggp= new DAOgroup(this);
    sharedPreference3 = getSharedPreferences(downnplace, 0);
    downoptionplace = sharedPreference3.getString("downloadflagplace","internal");
    Cursor bokgids = ggp.gids();
    do{
        allgids.add(bokgids.getString(0));
    }
    while(bokgids.moveToNext());

    for(int o=0;o<allgids.size();o++)
    {       
        ggp.unupdatduplicate(allgids.get(o));   
    }
    if(isNetworkConnected(this))
    {
        connection=true;
    }else 
    {
        connection=false;
    }

    new LoadData().execute();

}

这是showoffreebooks()函数的实现:

public void showoffreebooks()
{
     undelFREEBNME.clear();
     undelFREEAUTHNME.clear();
     undelFREEBCOVNME.clear();
     undelFREEbokprice.clear();
     undelFREEbokabout.clear();
     undelFREEbokpth.clear();
     undelFREEbokid.clear();
     undelFREEbokdel.clear();
     undelFREEbokgrp.clear();


     for(int  i=0;i<allFREEbokdel.size();i++)
    {
        ////////////////check my books if it's marked as deleted//////////////
        if(allFREEbokdel.get(i).equalsIgnoreCase("false"))
        {
            undelFREEBNME.add(allFREEBNME.get(i));
            undelFREEbokid.add(allFREEbokid.get(i));
            undelFREEAUTHNME.add(allFREEAUTHNME.get(i));
            undelFREEBCOVNME.add(allFREEBCOVNME.get(i));
            undelFREEbokpth .add(allFREEbokpth.get(i));
            undelFREEbokgrp.add(allFREEbokgrp.get(i));

        }
    }

     System.out.println("undelFREEBNME.size: "+undelFREEBNME.size());
     System.out.println("undelFREEBNME.size2: "+undelFREEBNME);

 for(int i=0;i<undelFREEBNME.size();i++)
    {
        /////////////check if it's in a group of books or not////////////
        if(undelFREEbokgrp.get(i).equalsIgnoreCase("0"))
        { 

            GETFREEBNME.add(undelFREEBNME.get(i));
            GETFREEbokid.add(undelFREEbokid.get(i));
            GETFREEBCOVNME.add(undelFREEBCOVNME.get(i));
            GETFREEbokgrp.add(undelFREEbokgrp.get(i));
            GETFREEbokpth .add(undelFREEbokpth.get(i));
            GETFREEAUTHNME.add(undelFREEAUTHNME.get(i));

        }
        else
        {
            offlinegetgroupboks(undelFREEbokgrp.get(i));

            if(offfrgdbct.get(i).equals("false"))  // it tells me the Exception is here 
            {
                GETFREEBCOVNME.add(offfrgcov.get(0));
                GETFREEbokid.add(offfrgid.get(0));
                GETFREEBNME.add(offfrgnme.get(0));
                GETFREEbokgrp.add(undelFREEbokgrp.get(i));
                GETFREEbokpth .add(undelFREEbokpth.get(i));
                GETFREEAUTHNME.add(undelFREEAUTHNME.get(i));

                updatedublicate(undelFREEbokgrp.get(i));
            }

        }
    }

offlinegetgroupboks(String id)函数的实现:

 public void offlinegetgroupboks(String id) {

        try{
            offfrgcov.clear();
            offfrgnme.clear();
            offfrgid.clear();
            offfrgdbct.clear();
            Cursor bookslist = ggp.fetchAlldata(id);

            if( bookslist != null ){

            do{
                offfrgcov.add(bookslist.getString(0));
                offfrgnme.add(bookslist.getString(1));
                offfrgid.add(bookslist.getString(2));
                offfrgdbct.add(bookslist.getString(3));

            }
            while(bookslist.moveToNext());
            }

        }catch(Exception e){
            e.printStackTrace();    
        }

    }

但它给了我以下异常:

09-05 22:34:12.554: E/AndroidRuntime(733): FATAL EXCEPTION: main
09-05 22:34:12.554: E/AndroidRuntime(733): java.lang.IndexOutOfBoundsException: Invalid index 2, size is 1
09-05 22:34:12.554: E/AndroidRuntime(733):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
09-05 22:34:12.554: E/AndroidRuntime(733):  at java.util.ArrayList.get(ArrayList.java:311)
09-05 22:34:12.554: E/AndroidRuntime(733):  at cx.hell.android.pdfview.TabFreeBooksActivity.showoffreebooks(TabFreeBooksActivity.java:382)
09-05 22:34:12.554: E/AndroidRuntime(733):  at cx.hell.android.pdfview.TabFreeBooksActivity$LoadData.onPostExecute(TabFreeBooksActivity.java:191)
09-05 22:34:12.554: E/AndroidRuntime(733):  at cx.hell.android.pdfview.TabFreeBooksActivity$LoadData.onPostExecute(TabFreeBooksActivity.java:1)
09-05 22:34:12.554: E/AndroidRuntime(733):  at android.os.AsyncTask.finish(AsyncTask.java:417)
09-05 22:34:12.554: E/AndroidRuntime(733):  at android.os.AsyncTask.access$300(AsyncTask.java:127)
09-05 22:34:12.554: E/AndroidRuntime(733):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
09-05 22:34:12.554: E/AndroidRuntime(733):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-05 22:34:12.554: E/AndroidRuntime(733):  at android.os.Looper.loop(Looper.java:123)
09-05 22:34:12.554: E/AndroidRuntime(733):  at android.app.ActivityThread.main(ActivityThread.java:4627)
09-05 22:34:12.554: E/AndroidRuntime(733):  at java.lang.reflect.Method.invokeNative(Native Method)
09-05 22:34:12.554: E/AndroidRuntime(733):  at java.lang.reflect.Method.invoke(Method.java:521)
09-05 22:34:12.554: E/AndroidRuntime(733):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-05 22:34:12.554: E/AndroidRuntime(733):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-05 22:34:12.554: E/AndroidRuntime(733):  at dalvik.system.NativeStart.main(Native Method)

有人可以帮忙吗? 提前致谢。

【问题讨论】:

  • 发布 AsyncTask 调用代码。此外,在这种情况下,从 doInBackground 返回 null 看起来是一个糟糕的设计决策。
  • 我编辑了它并添加了AsyncTask的调用
  • 另外,请指出实际引发异常的行。
  • 它告诉我异常在这一行:if(offfrgdbct.get(i).equals("false"))

标签: android sqlite cursor android-asynctask


【解决方案1】:

由于行号不存在,很难准确判断失败的位置:您应该显示准确的第 382 行。但是,您在 showoffreebooks 中有这段代码:

 undelFREEBNME.add(allFREEBNME.get(i));
            undelFREEbokid.add(allFREEbokid.get(i));
            undelFREEAUTHNME.add(allFREEAUTHNME.get(i));
            undelFREEBCOVNME.add(allFREEBCOVNME.get(i));
            undelFREEbokpth .add(allFREEbokpth.get(i));
            undelFREEbokgrp.add(allFREEbokgrp.get(i));

您假设allFREEBNMEallFREEbokidallFREEAUTHNMEallFREEBCOVNMEallFREEbokpthallFREEbokgrp 的大小都与allFREEbokdel 完全相同。显然,事实并非如此。在调用 get 之前检查这些变量的大小。

【讨论】:

  • 我检查了它们的尺寸,它们的尺寸都一样
  • 有一个特定的行崩溃:09-05 22:34:12.554: E/AndroidRuntime(733): at cx.hell.android.pdfview.TabFreeBooksActivity.showoffreebooks(TabFreeBooksActivity.java:382)。那条线是什么?
  • 行是:if(offfrgdbct.get(i).equals("false"))
  • 好吧,你已经知道了:offlinegetgroupboks 的实现没有用allFREEbokdel 中相同数量的项目填充ArrayList。在该行添加大小检查,或检查 offlinegetgroupboks 是否在做正确的事情。
  • allFREEbokdel 的大小为 7,而在 offlinegetgroupboks 中,数组的大小为 1,那么我该如何解决这个问题?
猜你喜欢
  • 2016-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多