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();
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity{
@Override
protected void onResume() {
super.onResume();
FileProvider provider = new FileProvider();
provider.testDbAccess();
}
}