I have a .java
file:
....
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
....
/* PackageInfo packageInfo =
getPackageManager().getPackageInfo(getPackageName(),0);
curVersionCode = packageInfo.versionCode;*/
PackageManager pm = getPackageManager();
String pn = getPackageName();
PackageInfo pi = pm.getPackageInfo(pn,0);
curVersionCode = pi.versionCode;
....
To understand the error i wrote the code in four lines. The two line code is in the comment as you see. Now that, this code doesn't work and gives an error of PackageManager.NameNotFoundException
here when i try to build. For clarity, I used this code in a different file and it caused no error there; so there is no need to worry about the package name. Now what is the cause behind this error? An image is also attached to make clear the error.
To solve this issue i also tried like these: this.getPackageManager()
, getBaseContext().getPackageManager()
and getApplicationContext().getPackageManager()
; But all in vain.