我在Android应用程序中无法访问sqlite数据库.我创建了一个包含sqliteOpenHelper实例的应用程序类,但是,每当我尝试在另一个活动中打开数据库时,它就会崩溃.
这是我的应用程序类:
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);}@OverrIDepublic voID onCreate(sqliteDatabase db) { db.execsql(DATABASE_CREATE);}@OverrIDepublic 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 VM05-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: main05-10 02:00:50.179: E/AndroIDRuntime(532): java.lang.RuntimeException: Unable to resume activity {com.John.seniorDesign/com.John.seniorDesign.Locationstabactivity}: java.lang.NullPointerException05-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.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.NullPointerException05-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
解决方法:
在GlobalVariables中,将上下文更改为新的LocationsData中的getApplicationContext()并从onCreate中删除上下文-此签名不存在.
编辑:
public class GlobalVariables extends Application { private LocationsData locations; @OverrIDe public voID onCreate() { locations = new LocationsData(getApplicationContext()); } public LocationsData getLocations() { return locations; }}
总结 以上是内存溢出为你收集整理的sqlite-Android中具有数据库故障的应用程序类全部内容,希望文章能够帮你解决sqlite-Android中具有数据库故障的应用程序类所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)