我想创建一个测验应用程序,其中包含1个问题和4个选项,我创建了一个数据库,使用ListView
将问题和选项从中提取到5个文本视图。我浏览了许多教程并使用stackoverflow中的页面使用ArrayList
和Cursor
创建了它,但我得到了一个
错误。
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.query(java.lang.String, java.lang.String[], java.lang.String, java.lang.String[], java.lang.String, java.lang.String, java.lang.String)' on a null object reference
at com.example.shark.shopapp.merge.getData(merge.java:26)
at com.example.shark.shopapp.MainActivity.onCreate(MainActivity.java:68)
它显示在Cursor.query()行中。 MainActivity:
setContentView(R.layout.ques_layout); db=openOrCreateDatabase("check1.db",SQLiteDatabase.CREATE_IF_NECESSARY,null);
db.execSQL("CREATE TABLE que(ques TEXT,optn1 TEXT,optn2 TEXT,optn3 TEXT,optn4 TEXT)");
ListView l1=(ListView)findViewById(R.id.listView2);
merge mer=new merge(this);
ArrayList<String> data=mer.getData();
l1.setAdapter(new ArrayAdapter<String>(this,R.layout.ques_layout,data));
db.close();
Merge.java:
public merge(Context c){ourContext=c;}
public ArrayList<String> getData() {
String[] columns = new String[]{ques, optn1, optn2, optn3, optn4};
Cursor c2=db.query(DATABASE_TABLE,columns,null,null,null,null,null);
ArrayList<String> result = new ArrayList<String>();
int ques = c2.getColumnIndex("_id");
int optn1 = c2.getColumnIndex("optn1");
int optn2 = c2.getColumnIndex("optn2");
int optn3 = c2.getColumnIndex("optn3");
int optn4 = c2.getColumnIndex("optn4");
for (c2.moveToFirst(); !c2.isAfterLast(); c2.moveToNext()) {
result.add(c2.getString(ques) + c2.getString(optn1) + c2.getString(optn2) + c2.getString(optn3) + c2.getString(optn4));
}
db.close();
return result;
}
}
答案 0 :(得分:1)
试试这个:
String strQuery =&#34; select from(选择option1作为表
中的选项
工会全部
从表格中选择option2作为选项
工会全部
从表格中选择option3作为选项
工会全部
选择option4作为表格中的选项)作为结果&#34;;
游标c2 = db.SelectQuery(strQuery);
String [] from = {&#34; option&#34;}; //您可以根据需要添加任意数量
在[] to to = {&#34; R.id.TextViewID&#34;}; //您可以根据需要添加任意数量。
SimpleCursorAdapter sca = new SimpleCursorAdapter(youractivityname.this,R.layout.listviewlayout,c2,from,to);
yourlistview.setAdapter(SCA);
答案 1 :(得分:0)
你可以这样做。
_cursor = _database.rawQuery("select * from "+YOUR_TABLE_NAME, null);
_cursor.moveToFirst();
while(_cursor.isAfterLast() == false){
_idDB = _cursor.getInt(_cursor.getColumnIndex(MySqliteHelper.READING_DETAILS_COLUMN_ID));
_ivrsDB = _cursor.getString(_cursor.getColumnIndex(MySqliteHelper.READING_DETAILS_COLUMN_IVRS_NUM));
_readingDB = _cursor.getString(_cursor.getColumnIndex(MySqliteHelper.READING_DETAILS_COLUMN_METER_READING));
_imageDB = _cursor.getString(_cursor.getColumnIndex(MySqliteHelper.READING_DETAILS_COLUMN_IMAGE));
_userIdDB = _cursor.getString(_cursor.getColumnIndex(MySqliteHelper.READING_DETAILS_COLUMN_USER_ID));
_latDB = _cursor.getString(_cursor.getColumnIndex(MySqliteHelper.READING_DETAILS_COLUMN_LAT));
_longDB = _cursor.getString(_cursor.getColumnIndex(MySqliteHelper.READING_DETAILS_COLUMN_LONG));
_dateDB = _cursor.getString(_cursor.getColumnIndex(MySqliteHelper.READING_DETAILS_COLUMN_DATE_TIME));
_mob1DB = _cursor.getString(_cursor.getColumnIndex(MySqliteHelper.READING_DETAILS_COLUMN_MOB_FIRST));
_mob2DB = _cursor.getString(_cursor.getColumnIndex(MySqliteHelper.READING_DETAILS_COLUMN_MOB_SECOND));
_reasonSelected = _cursor.getString(_cursor.getColumnIndex(MySqliteHelper.READING_DETAILS_COLUMN_REASON));
//need to call UPDATE WS
updateDataFromDB(context, _idDB , _ivrsDB , _readingDB, _imageDB, _userIdDB , _latDB , _longDB, _dateDB , _mob1DB , _mob2DB
,_reasonSelected);
System.out.println("Id from DB=" + _idDB);
System.out.println("IVRS NUMBER="+ _ivrsDB);
System.out.println("Reading="+ _readingDB);
//System.out.println("Image Path="+_cursor.getString(_cursor.getColumnIndex(MySqliteHelper.READING_DETAILS_COLUMN_IMAGE)));
System.out.println("USer ID="+_userIdDB);
System.out.println("LAT="+_latDB);
System.out.println("LONG="+_longDB);
System.out.println("DATE="+ _dateDB);
System.out.println("MOb 1="+_mob1DB);
System.out.println("Mob 2="+_mob2DB);
System.out.println("Reason from db =" + _reasonSelected);
_cursor.moveToNext();
}
_cursor.close();
答案 2 :(得分:0)
您的db
变量未在merge
初始化。
假设您使用SQLiteOpenHelper
,请致电,例如帮助对象上的getWritableDatabase()
获取可以分配给SQLiteDatabase
的{{1}}对象。