PackageManager namenotfoundexception
Asked Answered
Q

3

11

I'm new to android programming. I've created an app using Eclipse with a MainActivity.java and ClassFragment.java files. ClassFragment is a frangment. And I'm using newly updated appcompat library. I just want to implement a toast in my fragment with will show the version vode and version name of the app. I've used the following code PackageManager pInfo = getActivity().getPackageManager.getPackageInfo(getActivity().getPackageName, 0); But its showing java compilation error PackageManager namenotfoundexception. Pls help me out from this. Thanks in advance.

Quahog answered 10/1, 2016 at 19:30 Comment(0)
E
21

There is one behaviour change in Android API level 30 because of which we get NameNotFoundException when we call getPackageInfo on Android 11 even if the app/package app is installed on user's device. From Android API 30, we need to specify the package name in manifest in order to check if that package is installed on user's device.

<?xml version="1.0" encoding="utf-8"?>
<manifest 
 ...>
    <queries>
        <package android:name="com.example.Maps" />
    </queries>

    <application
       android:name=".Notes App"
    .../>

</manifest>
Eiger answered 11/11, 2021 at 9:29 Comment(2)
Reference link to the behavior change would be great, @ramakrishna-joshiFarsighted
@Farsighted Here is the official documentation link : developer.android.com/training/package-visibilityEiger
E
1
    PackageManager manager = getActivity().getPackageManager();
            PackageInfo info;
            try {
                info = manager.getPackageInfo(getActivity().getPackageName(), 0);
                String strVersion = "Version: " + info.versionName;
                String strVersionCode = " Build: "+ info.versionCode);
            } catch (NameNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
Enjoy answered 11/1, 2016 at 8:19 Comment(4)
Anyone have a clue why the error is showing up in the first place if the version name actually exists?Employ
@Htoo The logcat does show a name not found exception, but the name actually does exist. That's why my question was asking why the exception get raised even though the version name does existEmploy
@Employ You had an error in the method call getActivity().getPackageName. It was necessary so getActivity().getPackageName().Kosher
I don’t remember what project I was working on 😂 that was so long ago. But thank you !Employ
T
1

Since Android API level 30 you need to add this permission android.permission.QUERY_ALL_PACKAGES

more info: https://developer.android.com/training/package-visibility

Tilt answered 22/8, 2023 at 15:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.