广告也精彩
目录

前言

首先,这个问题有点神奇。

比如,出现问题的原因:

new.SQLiteOpenHelper()时,Context不能是Application的Context如果传入Application,会java.io.File android.content.Context.getDatabasePath(java.lang.String)' on a null object reference

但是,我项目中另一个系统APP一直传入的是Application的Context,也没有出现啥问题。

不过,按照上面的修改,运行后确实没有啥问题。

记录一下,方便自己查阅。

好记性不如烂笔头

正文

    mDBHelper = new DBHelper(MyApp.getContext(), "staff.db");

改为

    mDBHelper = new DBHelper(getContext(), "staff.db");

就可以解决这个问题。

下面是异常的日志的打印:

AndroidRuntime(24820): FATAL EXCEPTION: main
AndroidRuntime(24820): Process: com.la.providertwo, PID: 24820
AndroidRuntime(24820): java.lang.RuntimeException: Unable to get provider com.la.content.StatusProvider: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getDatabasePath(java.lang.String)' on a null object reference
AndroidRuntime(24820):     at android.app.ActivityThread.installProvider(ActivityThread.java:6396)
AndroidRuntime(24820):     at android.app.ActivityThread.installContentProviders(ActivityThread.java:5938)
AndroidRuntime(24820):     at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5853)
AndroidRuntime(24820):     at android.app.ActivityThread.access$1100(ActivityThread.java:199)
AndroidRuntime(24820):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
AndroidRuntime(24820):     at android.os.Handler.dispatchMessage(Handler.java:106)
AndroidRuntime(24820):     at android.os.Looper.loop(Looper.java:193)
AndroidRuntime(24820):     at android.app.ActivityThread.main(ActivityThread.java:6669)
AndroidRuntime(24820):     at java.lang.reflect.Method.invoke(Native Method)
AndroidRuntime(24820):     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
AndroidRuntime(24820):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:892)
AndroidRuntime(24820): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getDatabasePath(java.lang.String)' on a null object reference
AndroidRuntime(24820):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:352)
AndroidRuntime(24820):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:298)
AndroidRuntime(24820):     at com.la.content.StatusProvider.onCreate(StatusProvider.java:36)
AndroidRuntime(24820):     at android.content.ContentProvider.attachInfo(ContentProvider.java:1917)
AndroidRuntime(24820):     at android.content.ContentProvider.attachInfo(ContentProvider.java:1892)
AndroidRuntime(24820):     at android.app.ActivityThread.installProvider(ActivityThread.java:6391)
AndroidRuntime(24820):     ... 10 more
ActivityManager( 2419):   Force finishing activity com.la.providertwo/.MainActivity

参考文章

  1. getWritableDb失败——java.io.File android.content.Context.getDatabasePath(java.lang.String)'

 历史上的今天

版权声明 1、 本站名称: 91易搜
2、 本站网址: 91es.com3xcn.com
3、 本站文章: 部分来源于网络,仅供站长学习和参考,若侵权请留言
广告也精彩

相关文章

广告也精彩

暂无评论

评论审核已启用。您的评论可能需要一段时间后才能被显示。

暂无评论...

网站升级中

公告

近期网站升级中,可能存在一些bug。欢迎反馈 https://www.91es.com/we.html

本站域名

本站域名 : 91es.com3xcn.com。本站邮箱 : 站长邮箱 i@oorr.cn,通知邮箱we@oorr.cn ,如有更新,请看公告 。