访问具有不同上下文的数据库

Accessing DB with different Contexts

我正在摆弄 Android 中的数据库访问以查看事情是如何处理的。

我的 MainActivity.java 文件中有以下代码:

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 对象后查询和写入数据。

当我尝试使用 ContextgetContext() 返回的可写数据库时,它只是失败并返回 NullPointerException。它甚至没有开始创建数据库。即使我删除 getApplicationContext() 测试行的代码,也会出现症状。

这里的问题是,我正在尝试编写代码以从 FileProvider 中的数据库获取查询,但我无法从该文件访问 getApplicationContext()(它只会引发编译器错误) .

如果我在 MainActivity.java 文件中完成我的所有过程,我没有错误(我知道这不好,我这样做只是为了测试目的)。

我的问题是:

  1. 我应该如何以及在什么情况下创建我的数据库?
  2. 为什么我不能使用 FileProvider 中的 getApplicationContext()
  3. 我可以从创建数据库的另一个上下文访问数据库吗?据我所知,SQLite 数据库只是在应用程序自己的文件夹中的 android 文件系统中创建的一个文件。不同的Context有什么区别?
  4. 为什么我不能在 FileProvider.java 中使用 getContext() 返回的 Context 创建数据库?

--编辑--

这是错误的 logcat:

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.access0(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.access0(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 class(扩展 ContentProvider)中,如果我尝试 getContext() 除了 onCreate() 方法之外的任何其他地方,我得到一个空的语境。这就是为什么在我的 testDbAccess() 方法上获取数据库引用失败的原因。

我所做的是在我的 FileProvider class 中声明一个静态 FileDbHelper(扩展 SQLiteOpenHelper)。然后在 onCreate() 方法中,我创建了具有适当上下文的 FileDbHelper class。由于它是静态的,我现在可以稍后在同一个 class 中的任何地方使用这个对象。现在看起来有点像这样:

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() 上下文的 FileProvider.java 文件中创建数据库?

  • 实际上你可以,问题基本上是由于无法从我编写的新测试函数访问上下文本身引起的。不知道为什么,但就是这种情况:)

希望以后对其他人有所帮助。

更新

有关静态对象和内存泄漏的重要信息。

m0skit0 warned me about memory leak that is caused by static objects which holds reference to context. Detailed information can be found here。简而言之,它说

If you plan on keeping long-lived objects that need a context, remember the application object. You can obtain it easily by calling Context.getApplicationContext() or Activity.getApplication().

所以要小心那个。相应地更新了代码片段。这让未来的我免于头痛 :)

即使您已经回答了您的问题,我也想补充一些信息。

方法 getContext()getApplicationContext() 可以产生一个 NullPointerException,有时 classes 只是不能定义它们自己的 Context,而在那些情况下,APP会崩溃。

我认为将上下文显式传递给 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 provider = new FileProvider();

FileProvider provider = new FileProvider(this); 活动

FileProvider provider = new FileProvider(getContext()); 用于片段( 总是有上下文)

对于其他问题:数据库 class 在哪个上下文中工作没有区别。上下文主要是授予数据库助手访问设备的权限。