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.
PackageManager namenotfoundexception
Asked Answered
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>
Reference link to the behavior change would be great, @ramakrishna-joshi –
Farsighted
@Farsighted Here is the official documentation link : developer.android.com/training/package-visibility –
Eiger
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();
}
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 exist –
Employ
@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
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
© 2022 - 2024 — McMap. All rights reserved.