2012-05-10 109 views
3

我無法訪問我的Android應用程序中的SQLite數據庫。我創建了一個包含SQLiteOpenHelper實例的應用程序類,但是每當我嘗試在另一個活動中打開數據庫時,它都會崩潰。在Android中的應用程序類與數據庫問題

這裏是我的應用程序類:

public class GlobalVariables extends Application { 

    private LocationsData locations; 

    public void onCreate(Context context) { 
      locations = new LocationsData(context); 
    } 

    public LocationsData getLocations() { 
      return locations; 
    } 
} 

這裏是我SQLiteOpenHelper類:

public class LocationsData extends SQLiteOpenHelper{ 
private static final String DATABASE_NAME = "locations.db"; 
private static final int DATABASE_VERSION = 1; 

public static final String TABLE_LOCATIONS = "locations"; 
public static final String COLUMN_ID = "_id"; 
public static final String COLUMN_NICKNAME = "nickname"; 

private static final String DATABASE_CREATE = "create table " 
     + TABLE_LOCATIONS + "(" + COLUMN_ID 
     + " integer primary key autoincrement, " + COLUMN_NICKNAME 
     + " text not null);"; 

public LocationsData(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL(DATABASE_CREATE); 

} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOCATIONS); 
    onCreate(db); 

} 

}

,當我嘗試訪問該數據庫的另一個動作是這樣,我得到出錯:

@Override 
    public void onResume() 
    { 
     locationsData=((GlobalVariables)getApplication()).getLocations(); 
     SQLiteDatabase db = locationsData.getReadableDatabase(); 
     super.onResume(); 
    } 

任何幫助?謝謝!

Log Cat: 
05-10 02:00:50.146: D/AndroidRuntime(532): Shutting down VM 
05-10 02:00:50.146: W/dalvikvm(532): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
05-10 02:00:50.179: E/AndroidRuntime(532): FATAL EXCEPTION: main 
05-10 02:00:50.179: E/AndroidRuntime(532): java.lang.RuntimeException: Unable to resume activity {com.John.seniorDesign/com.John.seniorDesign.LocationsTabActivity}: java.lang.NullPointerException 
05-10 02:00:50.179: E/AndroidRuntime(532): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120) 
05-10 02:00:50.179: E/AndroidRuntime(532): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:138) 
05-10 02:00:50.179: E/AndroidRuntime(532): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339) 
05-10 02:00:50.179: E/AndroidRuntime(532): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:654) 
05-10 02:00:50.179: E/AndroidRuntime(532): at android.widget.TabHost.setCurrentTab(TabHost.java:326) 
05-10 02:00:50.179: E/AndroidRuntime(532): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:132) 
05-10 02:00:50.179: E/AndroidRuntime(532): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:456) 
05-10 02:00:50.179: E/AndroidRuntime(532): at android.view.View.performClick(View.java:2485) 
05-10 02:00:50.179: E/AndroidRuntime(532): at android.view.View$PerformClick.run(View.java:9080) 
05-10 02:00:50.179: E/AndroidRuntime(532): at android.os.Handler.handleCallback(Handler.java:587) 
05-10 02:00:50.179: E/AndroidRuntime(532): at android.os.Handler.dispatchMessage(Handler.java:92) 
05-10 02:00:50.179: E/AndroidRuntime(532): at android.os.Looper.loop(Looper.java:130) 
05-10 02:00:50.179: E/AndroidRuntime(532): at android.app.ActivityThread.main(ActivityThread.java:3683) 
05-10 02:00:50.179: E/AndroidRuntime(532): at java.lang.reflect.Method.invokeNative(Native Method) 
05-10 02:00:50.179: E/AndroidRuntime(532): at java.lang.reflect.Method.invoke(Method.java:507) 
05-10 02:00:50.179: E/AndroidRuntime(532): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
05-10 02:00:50.179: E/AndroidRuntime(532): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
05-10 02:00:50.179: E/AndroidRuntime(532): at dalvik.system.NativeStart.main(Native Method) 
05-10 02:00:50.179: E/AndroidRuntime(532): Caused by: java.lang.NullPointerException 
05-10 02:00:50.179: E/AndroidRuntime(532): at com.John.seniorDesign.LocationsTabActivity.onResume(LocationsTabActivity.java:75) 
05-10 02:00:50.179: E/AndroidRuntime(532): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150) 
05-10 02:00:50.179: E/AndroidRuntime(532): at android.app.Activity.performResume(Activity.java:3832) 
05-10 02:00:50.179: E/AndroidRuntime(532): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2110) 
05-10 02:00:50.179: E/AndroidRuntime(532): ... 17 more 
+0

從logcat發佈錯誤的堆棧跟蹤。 – superfell

+0

我認爲它可能與GlobalVariables類中的onCreate()調用中的上下文不正確有關。但我不確定... – Seuby

+1

發佈LocationsTabActivity.onResume的代碼 – superfell

回答

4

在GlobalVariables變化背景下getApplicationContext()在新LocationsData和刪除的onCreate語境 - 這個簽名不存在。

編輯:

public class GlobalVariables extends Application { 

    private LocationsData locations; 

    @Override 
    public void onCreate() { 
      locations = new LocationsData(getApplicationContext()); 
    } 

    public LocationsData getLocations() { 
      return locations; 
    } 
} 
+0

@Override會抓住這個。 – superfell

+0

當然,確實添加到我的答案。謝謝。 –

0

不知道是什麼原因,但在這裏是如何我通常實現我的分貝開/關功能:

在我的DB類:

public GroceryDB(Context ctx) { 
    this.mCtx = ctx; 
} 

public GroceryDB open() throws SQLException { 
    mDBHelper = new DBHelper(mCtx); 
    mDb = mDBHelper.getWritableDatabase(); 
    return this; 
} 

public void close() { 
    mDBHelper.close(); 
} 

然後,我把它叫做任何其他類:

mDbHelper = new GroceryDB(getActivity()); 
    mDbHelper.open(); 

希望這有助於!

0

在您的應用程序生命週期中,您總是希望確保您正在處理單個數據庫實例。看看這個answer到一個類似的問題。它給出了一些如何在應用程序中正確打開/關閉數據庫的例子。

相關問題