Android 使用不同上下文访问数据库
我在安卓系统中摆弄数据库访问,看看事情是如何处理的 我的MainActivity.java文件中有以下代码:Android 使用不同上下文访问数据库,android,nullpointerexception,android-sqlite,android-context,Android,Nullpointerexception,Android Sqlite,Android Context,我在安卓系统中摆弄数据库访问,看看事情是如何处理的 我的MainActivity.java文件中有以下代码: Log.v("test db acc", "start getApplicationContext test"); FileDbHelper dbHelper1 = new FileDbHelper(getApplicationContext()); SQLiteDatabase db1 = dbHelper1.getWritableDatabase(); Log.v("test db
Log.v("test db acc", "start getApplicationContext test");
FileDbHelper dbHelper1 = new FileDbHelper(getApplicationContext());
SQLiteDatabase db1 = dbHelper1.getWritableDatabase();
Log.v("test db acc", "success getApplicationContext test");
Log.v("test db acc", "start getContext test");
FileProvider provider = new FileProvider();
provider.testDbAccess();
Log.v("test db acc", "success getContext test");
这是provider.testDbAccess()函数的定义:
FileDbHelper dbHelper2 = new FileDbHelper(getContext());
SQLiteDatabase db2 = dbHelper2.getWritableDatabase();
首次尝试访问数据库成功,无任何错误。如果数据库不存在,它就会创建数据库,我可以在创建db1对象后查询和写入数据
当我尝试使用getContext()
返回的Context
获取可写数据库时,它只会因NullPointerException
而失败。它甚至没有开始创建数据库。即使删除getApplicationContext()
测试行的代码,也会出现症状
这里的问题是,我试图编写代码从FileProvider
中的数据库获取查询,但我无法从该文件访问getApplicationContext()
(这只会引发编译器错误)
如果我在MainActivity.java文件中执行所有流程,我就不会有任何错误(我知道这并不好,我这样做只是为了测试目的)
我的问题是:
FileProvider
中的getApplicationContext()
李>
Context
s有什么区别李>
getContext()
返回的Context
在FileProvider.java
中创建数据库08-02 16:03:55.628 7614-7614/com.permasse.apps.file.android E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to resume activity {com.permasse.apps.file.android/com.permasse.apps.file.android.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2575)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2089)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.permasse.apps.file.android.FileProvider.testDbAccess(FileProvider.java:120)
at com.permasse.apps.file.android.MainActivity.onResume(MainActivity.java:32)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1184)
at android.app.Activity.performResume(Activity.java:5082)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2565)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2603)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2089)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
下面是FileProvider.java的相关部分。原始代码包括查询生成器、uri匹配器等。我简化了代码以隔离问题
public class FileProvider extends ContentProvider {
private FileDbHelper dbHelper;
@Override
public boolean onCreate() {
dbHelper = new FileDbHelper(getContext());
return true;
}
public void testDbAccess() {
SQLiteDatabase db = dbHelper.getWritableDatabase(); //Line no 120
}
}
和MainActivity.java
public class MainActivity extends AppCompatActivity{
@Override
protected void onResume() {
super.onResume();
FileProvider provider = new FileProvider();
provider.testDbAccess(); //Line no 32
}
}
虽然我不知道为什么会这样,但我解决了这个问题 在我的
FileProvider
类(扩展了ContentProvider
)中,如果我尝试getContext()
除onCreate()
方法之外的任何其他方法,我会得到一个空上下文。这就是为什么在我的testDbAccess()方法上获取db引用失败的原因
我所做的是在我的FileProvider
类中声明了一个静态FileDbHelper
(它扩展了SQLiteOpenHelper
)。然后在onCreate()
方法中,我创建了具有适当上下文的FileDbHelper
类。因为它是静态的,所以我现在可以稍后在同一个类中的任何地方使用这个对象。现在看起来有点像这样:
public class FileProvider extends ContentProvider {
// Create static FileDbHelper
private static FileDbHelper dbHelper;
@Override
public boolean onCreate() {
// We can only access the context from onCreate() function, so we
// instantiate it here to use later on.
dbHelper = new FileDbHelper(this.getContext().getApplicationContext);
// Important explanation about context in bottom of the answer!!
return true;
}
// Then I can use it like this:
public void testDbAccess() {
// dbHelper was staticly declared within class and instantiated already
SQLiteDatabase db = dbHelper.getWritableDatabase();
// Then do whatever you want to do with the code
}
}
这也回答了我的问题:
我应该如何以及在哪个上下文中创建数据库?
- 在哪个上下文中创建数据库并不重要,只要能够获得适当的上下文,就可以访问它李>
为什么我不能使用来自FileProvider的getApplicationContext()
?
- 原来我能。正确的方法是
getContext().getApplicationContext()
,但您不必这样做来获取对数据库的引用李>
我可以从另一个创建数据库的上下文中访问该数据库吗?
- 是的,您可以在
getApplicationContext()
中创建数据库,并在getContext()中访问它
为什么我不能使用getContext()
context在FileProvider.java
文件中创建数据库?
- 事实上,这个问题基本上是由于无法从我编写的新测试函数中访问上下文本身造成的。不知道为什么,但情况就是这样:)
希望以后能对其他人有所帮助
更新
有关静态对象和内存泄漏的重要信息
警告我由保存上下文引用的静态对象引起的内存泄漏。详细信息可以找到。简而言之,它说
如果计划保留需要上下文的长寿对象,
记住应用程序对象。你可以打电话很容易地得到它
Context.getApplicationContext()或Activity.getApplication()
所以要小心这一点。相应地更新了代码段。这让未来的我不再头疼:)即使你已经回答了你的问题,我想补充一些信息
方法getContext()
和getApplicationContext()
可以产生NullPointerException
,有时类无法定义自己的上下文,在这些情况下,应用程序将崩溃
我认为将上下文显式地传递给ContentProvider
是一个好主意。这样你就可以确保你的应用程序不会崩溃
例如:
public class FileProvider extends ContentProvider {
Context context = null;
public FileProvider(Context context){
this.context = context;
}
// Create static FileDbHelper
private static FileDbHelper dbHelper;
@Override
public boolean onCreate() {
if(this.context != null{
dbHelper = new FileDbHelper(this.context);
}else{
// There is an error, notify the user and do something about it
return false;
}
return true;
}
// Then I can use it like this:
public void testDbAccess() {
// dbHelper was staticly declared within class and instantiated already
SQLiteDatabase db = dbHelper.getWritableDatabase();
// Then do whatever you want to do with the code
}
}
而在你看来,只要改变就行了
FileProvider=newfileprovider()代码>
到
FileProvider=newfileprovider(此文件)代码>用于活动
或者FileProvider provider=新的FileProvider(getContext())代码>用于片段(始终具有上下文)
对于其他问题:数据库类在哪个上下文中工作没有区别。上下文主要用于授予数据库帮助程序对设备的访问权限。我不确定您想做什么,但您可以将context
传递给FileProvider
class…新的FileProvider(getApplicationContext())
并在方法中使用context
而不是getContext()
,您可以从继承具有它们的类的类调用getContext()或getApplicationContext(),如Activity
,否则您必须传递它我基本上要做的是创建一个提供程序,稍后将绑定到装载机。我想创建一个数据库