保存在sqlite中的位置未显示在列表中

时间:2015-10-09 19:48:34

标签: java android sqlite google-maps

我是Android新手。 我试图从列表中的SQlite检索所有位置,但我面临错误。 实际上,我想在列表中显示存储在数据库中的纬度,经度和缩放值。 救救我!

Logcat错误

   10-10 00:52:35.257: E/AndroidRuntime(9868): FATAL EXCEPTION: main
10-10 00:52:35.257: E/AndroidRuntime(9868): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.haider.trymap/com.haider.trymap.SavedLocations}: java.lang.NullPointerException
10-10 00:52:35.257: E/AndroidRuntime(9868):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-10 00:52:35.257: E/AndroidRuntime(9868):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
10-10 00:52:35.257: E/AndroidRuntime(9868):     at android.app.ActivityThread.access$700(ActivityThread.java:157)
10-10 00:52:35.257: E/AndroidRuntime(9868):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
10-10 00:52:35.257: E/AndroidRuntime(9868):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-10 00:52:35.257: E/AndroidRuntime(9868):     at android.os.Looper.loop(Looper.java:176)
10-10 00:52:35.257: E/AndroidRuntime(9868):     at android.app.ActivityThread.main(ActivityThread.java:5317)
10-10 00:52:35.257: E/AndroidRuntime(9868):     at java.lang.reflect.Method.invokeNative(Native Method)
10-10 00:52:35.257: E/AndroidRuntime(9868):     at java.lang.reflect.Method.invoke(Method.java:511)
10-10 00:52:35.257: E/AndroidRuntime(9868):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
10-10 00:52:35.257: E/AndroidRuntime(9868):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
10-10 00:52:35.257: E/AndroidRuntime(9868):     at dalvik.system.NativeStart.main(Native Method)
10-10 00:52:35.257: E/AndroidRuntime(9868): Caused by: java.lang.NullPointerException
10-10 00:52:35.257: E/AndroidRuntime(9868):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:236)
10-10 00:52:35.257: E/AndroidRuntime(9868):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
10-10 00:52:35.257: E/AndroidRuntime(9868):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
10-10 00:52:35.257: E/AndroidRuntime(9868):     at com.haider.trymap.LocationsDB.<init>(LocationsDB.java:40)
10-10 00:52:35.257: E/AndroidRuntime(9868):     at com.haider.trymap.SavedLocations.<init>(SavedLocations.java:11)
10-10 00:52:35.257: E/AndroidRuntime(9868):     at java.lang.Class.newInstanceImpl(Native Method)
10-10 00:52:35.257: E/AndroidRuntime(9868):     at java.lang.Class.newInstance(Class.java:1319)
10-10 00:52:35.257: E/AndroidRuntime(9868):     at android.app.Instrumentation.newActivity(Instrumentation.java:1071)
10-10 00:52:35.257: E/AndroidRuntime(9868):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2171)
10-10 00:52:35.257: E/AndroidRuntime(9868):     ... 11 more

LocationsDB.java

public class LocationsDB extends SQLiteOpenHelper{
    /** Database name */
    private static String DBNAME = "locationmarkersqlite";

    /** Version number of the database */
    private static int VERSION = 1;

    /** Field 1 of the table locations, which is the primary key */
    public static final String FIELD_ROW_ID = "_id";

    /** Field 2 of the table locations, stores the latitude */
    public static final String FIELD_LAT = "lat";

    /** Field 3 of the table locations, stores the longitude*/
    public static final String FIELD_LNG = "lng";

    /** Field 4 of the table locations, stores the zoom level of map*/
    public static final String FIELD_ZOOM = "zom";

    /** A constant, stores the the table name */
    private static final String DATABASE_TABLE = "locations";

    /** An instance variable for SQLiteDatabase */
    private SQLiteDatabase mDB;  


    /** Constructor */
    public LocationsDB(Context context) {
        super(context, DBNAME, null, VERSION);  
        this.mDB = getWritableDatabase();
    }


    /** This is a callback method, invoked when the method getReadableDatabase() / getWritableDatabase() is called 
      * provided the database does not exists 
    * */
    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql =    "create table " + DATABASE_TABLE + " ( " +
                            FIELD_ROW_ID + " integer primary key autoincrement , " +
                            FIELD_LNG + " double , " +
                            FIELD_LAT + " double , " +
                            FIELD_ZOOM + " text " +
                        " ) ";

        db.execSQL(sql);
    }

    /** Inserts a new location to the table locations */
    public long insert(ContentValues contentValues){
        long rowID = mDB.insert(DATABASE_TABLE, null, contentValues);
        return rowID;

    }   


    /** Deletes all locations from the table */
    public int del(){
        int cnt = mDB.delete(DATABASE_TABLE, null , null);      
        return cnt;
    }

    /** Returns all the locations from the table */
    public Cursor getAllLocations(){
        return mDB.query(DATABASE_TABLE, new String[] { FIELD_ROW_ID,  FIELD_LAT , FIELD_LNG, FIELD_ZOOM } , null, null, null, null, null);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public ArrayList<String> getAllLocationz() {

        Cursor mcursor = mDB.rawQuery("SELECT contentValues FROM "
            + DATABASE_TABLE, null);

        ArrayList<String> result = new ArrayList<String>();

        if(mcursor!=null)
        {

            //move cursor to first result record
            mcursor.moveToFirst();

            do {
                result.add(mcursor.getString(mcursor.getColumnIndex("contentValues")));
            } while (mcursor.moveToNext());
        }
        return result;
    }


}

SavedLocations.java

public class SavedLocations extends ListActivity{

    LocationsDB lb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.saved_locations);

        ArrayList<String> data = lb.getAllLocationz(); 
          lb.close();

          setListAdapter(new ArrayAdapter<String>(this, 
                  android.R.layout.simple_list_item_1, 
                  data));


    }

0 个答案:

没有答案