Access to a database with different contexts

I play with db access in Android to find out how everything is handled.

I have the following code in the MainActivity.java file:

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");

And this is the definition of the provider.testDbAccess () function:

FileDbHelper dbHelper2 = new FileDbHelper(getContext());
SQLiteDatabase db2 = dbHelper2.getWritableDatabase();

The first attempt to attempt to access the database is successfully performed without any error. It creates a database if it does not exist, and I can query and write data after creating the db1 object.

When I try to get a writable database with Contextreturned using getContext(), it just fails with NullPointerException. He does not even begin to create a database. Symptoms occur even when I delete code for test lines getApplicationContext().

, FileProvider, getApplicationContext() ( ).

MainActivity.java, ( , , ).

:

  • ?
  • getApplicationContext() FileProvider?
  • , ? , SQLite - , Android . Context s?
  • FileProvider.java Context, getContext()?

- EDIT -

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.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

    }
}
+4
2

, , .

FileProvider ( ContentProvider), getContext() - , onCreate() , . db testDbAccess().

, FileDbHelper ( SQLiteOpenHelper) FileProvider. 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

    }
}

:

?

  • , db, , .

getApplicationContext() FileProvider?

  • , . getContext().getApplicationContext(), , db.

, ?

  • , db getApplicationContext() getContext()

FileProvider.java getContext() ?

  • , , , . , :)

, - .

​​ .

m0skit0 , , . . ,

, , . , Context.getApplicationContext() Activity.getApplication().

. . :)

+2

, .

getContext() getApplicationContext() NullPointerException, , , 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()); ( )

: , . .

+1

Source: https://habr.com/ru/post/1649886/


All Articles