开发者

nullpointerexception at sqlite populating a listview in android

开发者 https://www.devze.com 2023-04-12 14:47 出处:网络
i have this code: package com.rbrlnx.lugares; public class DataBaseHelper extends SQLiteOpenHelper { private static String DB_PATH = \"/data/data/com.rbrlnx.lugares/databases/\";

i have this code:

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", n开发者_如何学运维ull, 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);           


         }







        }

and this

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();
        }
}
}

And log cat give me this error

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

and

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

I don't know why, anyone can helps me please?


You don't created your DataBaseHelper instance. You need:

ayudabbdd = new DataBaseHelper(this);

before calling getNombers() method.


Use It

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

but i have no idea what the use of

 nombresC = ayudabbdd.getNombres(); 

and you have many spelling mistake while you are creating you table remove them also after check,

   like nol null in place of not null


You need to manually call openDataBase in onCreate prior to calling db.execSQL:

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

Similarly, your onUpgrade method needs to open the database first before calling db.execSQL otherwise you'll get the same type of error. And I don't think you should manually call onCreate from onUpgrade, figure out a different way to accomplish what you're trying there.

0

精彩评论

暂无评论...
验证码 换一张
取 消