In Kotlin, I'm building an app that uses the camera. However, it crashes whenever I click the camera button either in emulator or on actual device. I get the error below.
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
This error-type is non-specific and has been asked a number of times by Java (not kotlin) lang users. The solution has typically been name mismatch between auth in manifest file and usage in code function call, however none of the proposed solutions have worked for me.
Here is the snippet in my code where I make the 'null reference' call:
var currentPath: String? = null
val TAKE_PICTURE = 1
val SELECT_PICTURE = 2
fun dispatchCameraIntent() {
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
if(intent.resolveActivity(packageManager) != null){
var photoFile: File? = null
try {
photoFile = createImage()
}catch (e: IOException){
e.printStackTrace()
}
if (photoFile != null){
// you must create a content provider matching the authority
var photoUri = FileProvider.getUriForFile(this, "com.my_company.my_app.fileprovider", photoFile)
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri)
startActivityForResult(intent, TAKE_PICTURE)
}
}
}
fun createImage(): File{
val timeStamp = SimpleDateFormat("yyyyMMMM_HHmmss").format(Date())//getDateTimeInstance()
val imageName = "JPEG_"+timeStamp+"_"
var storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
var image = File.createTempFile(imageName, ".jpg", storageDir)
currentPath = image.absolutePath
return image
}
My xml file_paths file is at res>xml>file_paths.xml and is as follows:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="my_pictures"
path="SDCARD/Android/data/com.my_company.my_app/files/Pictures"/>
Here is my full AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my_company.my_app">
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera2" android:required="false"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".EmailSignIn" />
<activity android:name=".RegisterUser" />
<activity android:name=".ForgotPassword" />
<activity android:name=".ViewController">
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.my_company.my_app.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
</application>
Here is the trace:
08-04 18:03:51.830 28205-28205/com.my_company.my_app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.my_company.my_app, PID: 28205
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:584)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:558)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
at com.my_company.my_app.ViewController.dispatchCameraIntent(ViewController.kt:91)
at com.my_company.my_app.ViewController$onCreate$2.onClick(ViewController.kt:47)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)