一個ListView我有這樣的代碼:NullPointerException異常的sqlite的填充在android系統
package com.rbrlnx.lugares;
public class DataBaseHelper extends SQLiteOpenHelper {
private static String DB_PATH = "/data/data/com.rbrlnx.lugares/databases/";
private static final String DATABASE_NAME="db.db";
SQLiteDatabase db;
String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS lugares (" +
"_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"nombre text NOT NULL," +
"descripcion text NOL NULL,"+
"latitud real," +
"longitud real," +
"foto String);";
/*Primero se crea constructor, funcion onCreate, onUpgrade,Abrir y Cerrar*/
public DataBaseHelper(Context context){
super(context,DATABASE_NAME,null,1);
}
public void onCreate(SQLiteDatabase db){
db.execSQL(CREATE_TABLE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DELETE TABLE IF EXITS "+DATABASE_NAME+"");
onCreate(db);
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DATABASE_NAME;
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
public void close(){
db.close();
}
/*Despues metodos para añadir y obtener datos*/
public long addNombre(String nombre){
ContentValues cv = new ContentValues();
cv.put("nombre", nombre);
return db.insert("lugares", null, cv);
}
public long addDescripcion(String descripcion){
ContentValues cv = new ContentValues();
cv.put("descripcion", descripcion);
return db.insert("lugares", null, cv);
}
public long addLatitud(double latitud){
ContentValues cv = new ContentValues();
cv.put("latitud", latitud);
return db.insert("lugares", null, cv);
}
public long addLongitud(double longitud){
ContentValues cv = new ContentValues();
cv.put("longitud", longitud);
return db.insert("lugares", null, cv);
}
public long addFoto(String foto) {
ContentValues cv = new ContentValues();
cv.put("foto", foto);
return db.insert("lugares", null, cv);
}
public Cursor getNombres(){
Cursor respuesta = db.rawQuery("select nombres from lugares", null);
return respuesta;
}
public long addImagen(Uri directorioimagen){
String imagen = getRealPathFromURI(directorioimagen);
ContentValues cv = new ContentValues();
cv.put("foto", imagen);
return db.insert("lugares", null, cv);
}
}
這
public class listatab extends ListActivity{
Context context;
ListView listanombres;
DataBaseHelper ayudabbdd;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Cursor nombresC;
nombresC = ayudabbdd.getNombres();
startManagingCursor(nombresC);
ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.listatab, nombresC, new String[] { "nombre" }, new int[] { R.id.lista });
this.setListAdapter(adapter);
this.getListView().setTextFilterEnabled(true);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (ayudabbdd != null) {
ayudabbdd.close();
}
}
}
和日誌貓給我這個錯誤
10-10 21:25:38.115: ERROR/Database(26775): close() was never explicitly called on database '/data/data/com.rbrlnx.lugares/databases/db.db'
10-10 21:25:38.115: ERROR/Database(26775): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
和
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): FATAL EXCEPTION: main
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rbrlnx.lugares/com.rbrlnx.lugares.listatab}: java.lang.NullPointerException
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1692)
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:656)
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): at android.widget.TabHost.setCurrentTab(TabHost.java:326)
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:132)
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:458)
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): at android.view.View.performClick(View.java:2533)
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): at android.view.View$PerformClick.run(View.java:9320)
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): at android.os.Handler.handleCallback(Handler.java:587)
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): at android.os.Handler.dispatchMessage(Handler.java:92)
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): at android.os.Looper.loop(Looper.java:150)
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): at android.app.ActivityThread.main(ActivityThread.java:4389)
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): at java.lang.reflect.Method.invokeNative(Native Method)
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): at java.lang.reflect.Method.invoke(Method.java:507)
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): at dalvik.system.NativeStart.main(Native Method)
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): Caused by: java.lang.NullPointerException
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): at com.rbrlnx.lugares.listatab.onCreate(listatab.java:21)
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
10-10 21:35:48.831: ERROR/AndroidRuntime(27458): ... 18 more
我不知道爲什麼,任何人都可以幫助我嗎?
當您重新引發自己的異常時,更容易調試android應用程序線程,因爲android活動只是拋出RuntimeExceptions而不保留堆棧跟蹤。 – Sibbo
也,你已經在你的查詢中寫入'NOL NULL' – Sibbo
我怎樣才能調試android應用程序,當我拋出異常? db.db有數據。 我刪除查詢中的非空 – colymore