大家好,我正在封装一个用于从本地ip数据库中提取数据的应用程序,在该应用程序中,从提取文章中按下按钮时启动应用程序崩溃,插入代码错误是mainactivity2上的一个子调用:
public List<String> getAllLabels(){
List<String> labels = new ArrayList();
String selectQuery = "SELECT * FROM " + KEY_ARTICOLO;
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
labels.add(cursor.getString(1));
}while (cursor.moveToNext());
}
return labels;
}
错误是:
Process: spa.a1926.federighi.blancmariclo, PID: 8203
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])' on a null object reference
at spa.a1926.federighi.blancmariclo.MainActivity2$GestioneDB.getAllLabels(MainActivity2.java:131)
at spa.a1926.federighi.blancmariclo.MainActivity2$1.onClick(MainActivity2.java:50)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
10-30 10:53:22.815 1536-3316/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
10-30 10:53:22.815 1536-3316/system_process E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
main(mainactivity2)中的调用是:
public void onClick(View view) {
GestioneDB db = new GestioneDB(getApplicationContext());
List<String> parolemenu = db.getAllLabels();
ArrayAdapter dataAdapter = new ArrayAdapter(MainActivity2.this, android.R.layout.simple_spinner_item, parolemenu);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
}
});