我想從我創建的數據庫中使用類擴展到Sqliteopenhelper類,並將其放到列表視圖中的數據,但我從數據庫使用fetch()函數時出現錯誤在我的活動課上課。請幫我解釋爲什麼在「cursor = demo1.fetch();」中出現錯誤只有線.....但是當我包括行demo1.adddata()這是用來在數據庫中添加數據線,所述應用程序運行而不給予任何錯誤....無法從自己創建的SQLite數據庫中獲取數據android
mainactivity.java
public class mainactivity extends Activity{
Listview list;
Cursor cursor;
database demo1;
String[] str;
int count;
adapterdemo adapter;
protected void onCreate(Bundle savedInstance){
super.onCreate(savedInstance);
setContentView(R.layout.list);
demo1 = new database(getApplicationContext());
//demo1.adddata();
cursor = demo1.fetch();
count = cursor.getCount();
str = new String[count];
cursor.moveToFirst();
for(int i=0;i<count;i++)
{
str[i] = cursor.getString(0);
Toast.makeText(getApplicationContext(), str[i], 3000).show();
cursor.moveToNext();
}
adapter= new adapterdemo(this, str);
list.setAdapter(adapter);
}}
database.java
public class database extends SQLiteOpenHelper {
SQLiteDatabase sql;
public database(Context context) {
super(context, "Template", null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
String query = "Create table smstemp(sms_Template Text)";
arg0.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
public void adddata() {
// TODO Auto-generated method stub
sql = this.getReadableDatabase();
ContentValues cv = new ContentValues();
cv.put("sms_Template", "wait....");
sql.insert("smstemp", null, cv);
}
public Cursor fetch() {
// TODO Auto-generated method stub
Cursor c = sql.query("smstemp", new String[]{"sms_Template"}, null,null, null, null, null);
return c;
}
}
你能提供錯誤日誌嗎? – prabhat
可能在光標處獲得NPE c = sql.query(..) – prabhat
sql = this.getReadableDatabase();將此ling移動到onCreate()。 – prabhat