从Sqlite检索数据时出现空指针异常

时间:2012-01-12 04:17:44

标签: android sqlite cursor

我正在通过游标从sqlite到基础适配器检索数据。

main.java

SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor c=db.rawQuery("select * from budget",null);
      while (c.moveToNext()) {



        String tes0 = Integer.toString(c.getInt(c.getColumnIndex("_id")));
        String tes1 = Double.toString(c.getDouble(c.getColumnIndex("material_actual")));
        tes2 = c.getString(c.getColumnIndex("start_date"));
        tes3 = c.getString(c.getColumnIndex("end_date"));

        String[] v0 = new String[] { tes0 };
        String[] v01 = new String[] { tes1 };
        String[] v02 = new String[] { tes2 };
        String[] v03 = new String[] { tes3 };

            Adapter_ListView adapter = new Adapter_ListView(getBaseContext(),
                v01, v02 , v03, theTotal); //string[]
            TaskList.setAdapter(adapter);

}

然后,Adapter_listView.java

public class Adapter_ListView extends BaseAdapter {
private int count;
private Context context;

private String[] string1;
private String[] string2;
private String[] string3;

private String con1;
private String con2;
private String con3;


public Adapter_ListView(Context baseContext, String[] v01, String[] v02, String[] v03, int theTotal) {
       this.count   = theTotal;
       this.context = baseContext;
       this.string1 = v01;
       this.string2 = v02;
       this.string3 = v03;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return count;
}

@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View contentView, ViewGroup arg2) {
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    contentView = inflater.inflate(R.layout.layout_inflate_list2, null);

    TextView title = (TextView)contentView.findViewById(R.id.inflate_title);
    TextView body = (TextView)contentView.findViewById(R.id.inflate_body);
    TextView sub = (TextView)contentView.findViewById(R.id.inflate_sub);


    title.setText(string1[position]);
    body.setText(string2[position]);
    sub.setText(string3[position]);


    return contentView;
}
}

从此代码始终错误 - >如果执行此代码,则为ArrayOuOfBound

title.setText(string1[position]);

我怎么能解决它?

4 个答案:

答案 0 :(得分:2)

您需要在检索的活动中打开数据库,或将数据插入SQLite database。还要确保在完成后关闭它。

final DatabaseHelper m = new DatabaseHelper(this);
 m.open();

在你的onDestroy或暂停。呼叫

m.close();

这可能是个问题,因为我看不到你在代码中打开数据库的位置。

编辑:

DatabaseHelper课程中。创建一个方法。

public void open(){
db.open();
}

public void close(){
db.close()
}

然后,您将拥有在您需要插入和检索信息的活动中关闭和打开数据库的方法。

答案 1 :(得分:1)

要从光标检索数据,首先必须转到光标中的第一行数据。

有些代码如下:

SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor c=db.rawQuery("select * from budget",null);
if (c!=null) c.moveToFirst();
      while (c.moveToNext()) {
...
}
祝你好运! :d

答案 2 :(得分:0)

您似乎想将结果集加载到列表中,如果是这样,请更改您的代码以在循环外设置适配器:

SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor c=db.rawQuery("select * from budget",null);
int theTotal=c.getCount();
 String[] v0 = new String[theTotal];
        String[] v01 = new String[theTotal];
        String[] v02 = new String[theTotal];
        String[] v03 = new String[theTotal];

      int i=0;
      if (c!=null) c.moveToFirst();
      while (c.moveToNext()) {



        String tes0 = Integer.toString(c.getInt(c.getColumnIndex("_id")));
        String tes1 = Double.toString(c.getDouble(c.getColumnIndex("material_actual")));
        tes2 = c.getString(c.getColumnIndex("start_date"));
        tes3 = c.getString(c.getColumnIndex("end_date"));

        v0[i] =tes0 ;
        v01[i] = tes1 ;
        v02[i] = tes2 ;
        v03[i] = tes3 ;



}

Adapter_ListView adapter = new Adapter_ListView(getBaseContext(),
                v01, v02 , v03, theTotal); //string[]
            TaskList.setAdapter(adapter);

答案 3 :(得分:0)

试试这个:

<强> main.java

SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor c=db.rawQuery("select * from budget",null);
if(c.getCount()>0)
{
    int i=0; 
    int clength=c.getCount();
    String[] v0=new String[clength];
    String[] v1=new String[clength];
    String[] v2=new String[clength];
    String[] v3=new String[clength];

    while (c.moveToNext()) 
    {        
        String tes0 = Integer.toString(c.getInt(c.getColumnIndex("_id")));
        String tes1 = Double.toString(c.getDouble(c.getColumnIndex("material_actual")));
        tes2 = c.getString(c.getColumnIndex("start_date"));
        tes3 = c.getString(c.getColumnIndex("end_date"));

        v0[i]=tes0;
        v01[i]=tes1;
        v01[i]=tes2;
        v01[i]=tes3;
        i++;    
    }
    Adapter_ListView adapter = new Adapter_ListView(getBaseContext(),v01, v02 , v03, theTotal); //string[]
    TaskList.setAdapter(adapter);
}