为什么会出现此NullPointerException:空对象引用错误?

时间:2018-08-04 23:44:19

标签: java android android-studio kotlin android-camera2

在Kotlin中,我正在构建一个使用相机的应用程序。但是,每当我在模拟器或实际设备上单击“相机”按钮时,它就会崩溃。我收到以下错误。

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

此错误类型是非特定的,并且Java(不是kotlin)lang用户多次询问。解决方案通常是清单文件中的auth和代码函数调用中的用法之间的名称不匹配,但是所提出的解决方案都没有对我有用。

这是我的代码中调用“空引用”的代码段:

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
    }

我的xml file_paths文件位于res> xml> file_paths.xml中,如下所示:

<?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"/>

这是我完整的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>

这是痕迹:

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)

1 个答案:

答案 0 :(得分:0)

这是导致问题的代码。它在FileProvider类中。

    final ProviderInfo info = context.getPackageManager()
            .resolveContentProvider(authority, PackageManager.GET_META_DATA);
    final XmlResourceParser in = info.loadXmlMetaData(
            context.getPackageManager(), META_DATA_FILE_PROVIDER_PATHS);

堆栈跟踪清楚地表明NPE正在发生loadXmlMetaData调用,并且发生的原因是infonull。 (在FileProvider类中,该方法只有一个调用站点……)

此证据表明某事与您定义FileProvider的方式有误。

  • 检查/重新检查{{3}}。
  • 检查清单中的拼写错误或不匹配等。