来自SQLite数据库的Android填充文本字符串
我使用字符串填充listview上的textview来自SQLite数据库的Android填充文本字符串,android,arrays,string,sqlite,Android,Arrays,String,Sqlite,我使用字符串填充listview上的textview String[] text1 = { "Afghanistan", "Algeria" ,"Fred"}; 我想用数据库中的数据替换字符串数组中的3个字符串。我试过以下方法 String text1[]; DBAdapter db = new DBAdapter(this); db.open(); Cursor c = db.getAsset3(); int counter = 0; whil
String[] text1 = { "Afghanistan", "Algeria" ,"Fred"};
我想用数据库中的数据替换字符串数组中的3个字符串。我试过以下方法
String text1[];
DBAdapter db = new DBAdapter(this);
db.open();
Cursor c = db.getAsset3();
int counter = 0;
while (c.moveToNext()) {
text1[counter]=c.getString(0);
counter++;
}
从DBAdapter获取资产3
public Cursor getAsset3() throws SQLException
{
Cursor mCursor =
db.query(true, "SURVDAT", new String[] {KEY_SR1,KEY_SR2,KEY_SR3,KEY_SR4,KEY_SR5,KEY_SR6,KEY_SR7}, null, null,null, null, null, null);
if (mCursor != null) {
//mCursor.moveToFirst();
}
return mCursor;
}
当我运行应用程序时,会崩溃,说空点异常
知道我哪里出错了吗
谢谢你的帮助
在第二个代码段中标记您没有初始化
text1
,因此当您键入text1[counter]=c.getString(0)时
您正在尝试获取计数器的第个索引,该索引为null
你应该这样做
DBAdapter db=新的DBAdapter(此);
db.open();
游标c=db.getAsset3();
字符串text1[]=新字符串[c.getCount()];
对于(int i=0;cursor.moveToNext();++i)
{
text1[i]=c.getString(0);
}
cursor.close();
您在哪一行收到异常?NPE最有可能是由于未正确初始化变量引起的。感谢您的帮助,我在字符串text1[]=new string(c.getCount())行上出现错误“类型不匹配:无法将字符串转换为字符串”;再次感谢,我计算出需要将行更改为字符串[]text1=新字符串[c.getCount()];而且works@MarkBarr是的,我的打字错误。