【问题标题】:App crash while getting data from database从数据库获取数据时应用程序崩溃
【发布时间】:2013-05-10 15:52:54
【问题描述】:

首先我很抱歉我的英语不太好。我目前正在使用 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 没有显示任何错误,但是当我尝试在手机上运行此应用程序时,应用程序在获取数据时崩溃。有人知道这是怎么回事吗?

【问题讨论】:

  • 请发布您遇到的错误......
  • logcat 没有显示任何错误

标签: android database sqlite crash


【解决方案1】:

请务必发布您的 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());

【讨论】:

  • 看看我的扩展答案。通过使用 ArrayList,您不需要事先知道大小,但仍然可以添加项目...
  • 不客气。一个提示,不过:学习使用 logcat!我很确定那里有一些信息。
【解决方案2】:

你必须在使用它之前初始化所有的数组。

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();  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 2017-10-13
    • 1970-01-01
    • 2014-12-30
    • 2014-12-01
    相关资源
    最近更新 更多