2011-10-10 55 views
0

一個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 

我不知道爲什麼,任何人都可以幫助我嗎?

+0

當您重新引發自己的異常時,更容易調試android應用程序線程,因爲android活動只是拋出RuntimeExceptions而不保留堆棧跟蹤。 – Sibbo

+0

也,你已經在你的查詢中寫入'NOL NULL' – Sibbo

+0

我怎樣才能調試android應用程序,當我拋出異常? db.db有數據。 我刪除查詢中的非空 – colymore

回答

0

您沒有創建DataBaseHelper實例。您需要:

ayudabbdd = new DataBaseHelper(this); 

在致電getNombers()方法之前。

+0

我刪除了這個行錯誤,但與該行是相同的結果 – colymore

-1

您需要手動調用onCreateopenDataBase呼籲db.execSQL之前:

 public void onCreate(SQLiteDatabase db){ 
     try { 
      openDataBase(); 
      db.execSQL(CREATE_TABLE); 
     } catch (Exception e) { 
      // handle exception 
     }    
    } 

同樣,你onUpgrade方法需要先調用db.execSQL否則你會得到相同類型的錯誤之前打開數據庫。我不認爲你應該從onUpgrade手動撥打onCreate,找出一種不同的方式來完成你在那裏嘗試的東西。

+0

錯誤/ AndroidRuntime(380):java.lang.RuntimeException:ComponentInfo {com.rbrlnx.lugares/com.rbrlnx.lugares.listatab}:java.lang.NullPointerException 錯誤/ AndroidRuntime(380):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 10-10 23:21:09.460:ERROR/AndroidRuntime(380):引起:java.lang.NullPointerException 10-10 23:21:09.460:ERROR/AndroidRuntime(380):at com.rbrlnx.lugares.DataBaseHelper.getNombres(DataBaseHelper.java:111) 10-10 23:21:09.460:ERRO R/AndroidRuntime(380):在com.rbrlnx.lugares.listatab.onCreate(listatab.java:22) 相同的結果.. – colymore

+0

我已經用sqlite編輯器打開了db.db,並且手動添加了註冊表,並且在eclipse中如果在文件探索中使用de ddms透視圖,我可以看到db.db有很多註冊表。 – colymore

0

使用它

DataBaseHelper ayudabbdd=new DataBaseHelper(this); 
    SQLiteDatabase db=ayudabbdd.getReadableDatabase(); 
    Cursor cur=db.<Method Name which you want to use>; 

,但我不知道有什麼用的

nombresC = ayudabbdd.getNombres(); 

和你有很多拼寫錯誤,而你正在創建你的表刪除它們還檢查後,

like nol null in place of not null 
相關問題