从数据库获取数据时应用程序崩溃

时间:2013-05-10 15:52:54

标签: android database sqlite crash

首先,对不起,我说英语不太好。我正在用eclipse开发一个Android应用程序,从数据库中获取数据时遇到问题。这是从数据库中读取数据的方法。我把这个方法放在一个名为SQLiteAdapter的类中:

 public Cursor getQuestionEachLevel(long levelId)
 {
     return sqLiteDatabase.query(MYDATABASE_TABLE, new String[]{KEY_ROWID, KEY_ANSWER,        
     KEY_QUESTION, KEY_HINT, KEY_FLAG, KEY_LEVEL}, KEY_LEVEL + "=" + levelId,
     null, null, null, null, null);
 }

这是将调用该方法并获取数据的代码。我将此代码放在另一个类中。

    int x=0;
    String getId[] = null;
    String getAnswer[] = null;
    String getQuestion[] = null;
    String getHint[] = null;
    String getFlag[]= null;
    String getLevel[]= null;

            mySQLiteAdapter = new SQLiteAdapter(this);
            mySQLiteAdapter.openToRead();
            Cursor c = mySQLiteAdapter.getQuestionEachLevel(getLevelKey);
            if(c.moveToFirst()){
        do{
            getId[x] = c.getString(0);
            getAnswer[x] = c.getString(1);
            getQuestion[x] = c.getString(2);
            getHint[x] = c.getString(3);
            getFlag[x] = c.getString(4);
            getLevel[x] = c.getString(5);
            x++;
        }while(c.moveToNext());
    }
    mySQLiteAdapter.close();  

我想将数据库列中的每个数据放入特定的变量中,因为我稍后会使用它。 Eclipse没有显示任何错误,但是当我尝试在手机上运行此应用程序时,应用程序在获取数据时崩溃。有人知道这有什么问题吗?

2 个答案:

答案 0 :(得分:2)

一定要发布你的logcat。但与此同时

int x=0;
String getId[] = null;
String getAnswer[] = null;
String getQuestion[] = null;
String getHint[] = null;
String getFlag[]= null;
String getLevel[]= null;

之后

do{
        getId[x] = c.getString(0);
        getAnswer[x] = c.getString(1);
        getQuestion[x] = c.getString(2);
        getHint[x] = c.getString(3);
        getFlag[x] = c.getString(4);
        getLevel[x] = c.getString(5);
        x++;
    }while(c.moveToNext());

总是很有问题...(你的数组没有被初始化,你正试图访问它们。我建议这个改变:

    int x=0;
List<String> getId = new ArrayList<String>();
List<String> getAnswer = new ArrayList<String>();
List<String>getQuestion = new ArrayList<String>();
List<String> getHint = new ArrayList<String>();
List<String>getFlag= new ArrayList<String>();
List<String>getLevel = new ArrayList<String>();

之后

 do{
        getId.add( c.getString(0));
        getAnswer.add(  c.getString(1));
        getQuestion.add(c.getString(2));
        getHint.add(c.getString(3));
        getFlag.add(c.getString(4));
        getLevel.add(c.getString(5));
        x++;
    }while(c.moveToNext());

答案 1 :(得分:0)

您必须在使用之前初始化所有阵列。

int x=0;
String getId[] = null;
String getAnswer[] = null;
String getQuestion[] = null;
String getHint[] = null;
String getFlag[]= null;
String getLevel[]= null;

       mySQLiteAdapter = new SQLiteAdapter(this);
       mySQLiteAdapter.openToRead();
       Cursor c = mySQLiteAdapter.getQuestionEachLevel(getLevelKey);
       if(c.moveToFirst()){
       int numOfRows = c.getCount();
       getId = new String[numOfRows];
       getAnswer = new String[numOfRows];
       getQuestion = new String[numOfRows];
       getHint = new String[numOfRows];
       getFlag = new String[numOfRows];
       getLevel = new String[numOfRows];
    do{
        getId[x] = c.getString(0);
        getAnswer[x] = c.getString(1);
        getQuestion[x] = c.getString(2);
        getHint[x] = c.getString(3);
        getFlag[x] = c.getString(4);
        getLevel[x] = c.getString(5);
        x++;
    }while(c.moveToNext());
}
mySQLiteAdapter.close();  
相关问题