I'm getting below crash in Android 11 OnePlus Device, How to fix this?

944 views Asked by At

Getting application info from package manager leads to app crash, why this has occurred and how to fix it?

Error code

Fatal Exception: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:641)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)
Caused by android.content.pm.PackageManager$NameNotFoundException: com.mi.android.globalfileexplorer
   at android.app.ApplicationPackageManager.getApplicationInfoAsUser(ApplicationPackageManager.java:428)
   at android.app.ApplicationPackageManager.getApplicationInfo(ApplicationPackageManager.java:417)
 at com.example.mediapicker.ui.FolderListFragment.setMenuItems(FolderListFragment.kt:92)
   at com.example.mediapicker.ui.FolderListFragment.onPrepareOptionsMenu(FolderListFragment.kt:84)
   at androidx.fragment.app.Fragment.performPrepareOptionsMenu(Fragment.java:3112)
   at androidx.fragment.app.FragmentManager.dispatchPrepareOptionsMenu(FragmentManager.java:3212)
   at androidx.fragment.app.Fragment.performPrepareOptionsMenu(Fragment.java:3114)

App Code

 private fun setMenuItems(apps: MutableList<String>, menu: Menu) {
    menu.clear()
    val pm = requireContext().packageManager
    apps.forEachIndexed { index, packageName ->
        val appInfo = pm.getApplicationInfo(
            packageName,
            PackageManager.GET_META_DATA
        )
        menu.add(
            0, index, Menu.NONE, pm.getApplicationLabel(appInfo)
        ).icon = pm.getApplicationIcon(packageName)
        if (menu is MenuBuilder) {
            menu.setOptionalIconsVisible(true)
        }
    }
}

Manifest file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mediapicker">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />

<application>
    <activity
        android:name=".ui.MediaPickerActivity"
        android:label="@string/title_activity_media_picker"
        android:theme="@style/ef_ImagePickerTheme"/>
</application>

<queries>
    <intent>
        <action android:name="android.intent.action.PICK" />
        <data android:mimeType="video/*, image/*" />
    </intent>
</queries>

I'm running the above code in my application and I'm facing this crash in One Plus devices how to fix this?

1

There are 1 answers

0
Jameido On BEST ANSWER

As stated in the documentation

getApplicationInfo Throws PackageManager.NameNotFoundException if a package with the given name cannot be found on the system.

In your case you are looking for com.mi.android.globalfileexplorer which is the Xiaomi file explorer app that is not installed on your OnePlus device.

Handle the exception and avoid adding the item to the menĂ¹ if thrown.