Reputation: 6436
I want to get the version number of my application but the application crashes upon load when I try it.
int currentVersionCode;
try {
currentVersionCode = this.getPackageManager().getPackageInfo(
this.getApplicationInfo().packageName, 0).versionCode;
Toast.makeText(this, currentVersionCode, Toast.LENGTH_SHORT).show();
} catch (NameNotFoundException e) {
return;
}
LogCat:
04-05 16:01:43.108: D/LocationManager(1109): Constructor: service = android.location.ILocationManager$Stub$Proxy@44c0f748
04-05 16:01:43.198: W/ResourceType(1109): No package identifier when getting name for resource number 0x00000001
04-05 16:01:43.208: D/AndroidRuntime(1109): Shutting down VM
04-05 16:01:43.208: W/dalvikvm(1109): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
04-05 16:01:43.208: E/AndroidRuntime(1109): Uncaught handler: thread main exiting due to uncaught exception
04-05 16:01:43.228: E/AndroidRuntime(1109): java.lang.RuntimeException: Unable to start activity ComponentInfo{weather.right.now/weather.right.nowActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x1
04-05 16:01:43.228: E/AndroidRuntime(1109): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
04-05 16:01:43.228: E/AndroidRuntime(1109): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
04-05 16:01:43.228: E/AndroidRuntime(1109): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
04-05 16:01:43.228: E/AndroidRuntime(1109): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
04-05 16:01:43.228: E/AndroidRuntime(1109): at android.os.Handler.dispatchMessage(Handler.java:99)
04-05 16:01:43.228: E/AndroidRuntime(1109): at android.os.Looper.loop(Looper.java:123)
04-05 16:01:43.228: E/AndroidRuntime(1109): at android.app.ActivityThread.main(ActivityThread.java:4363)
04-05 16:01:43.228: E/AndroidRuntime(1109): at java.lang.reflect.Method.invokeNative(Native Method)
04-05 16:01:43.228: E/AndroidRuntime(1109): at java.lang.reflect.Method.invoke(Method.java:521)
04-05 16:01:43.228: E/AndroidRuntime(1109): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
04-05 16:01:43.228: E/AndroidRuntime(1109): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
04-05 16:01:43.228: E/AndroidRuntime(1109): at dalvik.system.NativeStart.main(Native Method)
04-05 16:01:43.228: E/AndroidRuntime(1109): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x1
04-05 16:01:43.228: E/AndroidRuntime(1109): at android.content.res.Resources.getText(Resources.java:200)
04-05 16:01:43.228: E/AndroidRuntime(1109): at android.widget.Toast.makeText(Toast.java:258)
04-05 16:01:43.228: E/AndroidRuntime(1109): at weather.right.nowActivity.onCreate(nowActivity.java:58)
04-05 16:01:43.228: E/AndroidRuntime(1109): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-05 16:01:43.228: E/AndroidRuntime(1109): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
04-05 16:01:43.228: E/AndroidRuntime(1109): ... 11 more
04-05 16:01:43.248: I/dalvikvm(1109): threadid=7: reacting to signal 3
04-05 16:01:43.258: I/dalvikvm(1109): Wrote stack trace to '/data/anr/traces.txt'
How can I fix my problem?
Thanks in advance.
Upvotes: 1
Views: 402
Reputation: 23873
If you want the versionName, then:
String currentVersionName;
try {
currentVersionName = this.getPackageManager().getPackageInfo(
this.getApplicationInfo().packageName, 0).versionName;
Toast.makeText(this, currentVersionName, Toast.LENGTH_SHORT).show();
} catch (NameNotFoundException e) {
e.printStackTrace();
}
is more appropriate, that would work if you had
android:versionCode="1"
android:versionName="Mickey Mouse"
It would show "Mickey Mouse". The answer you accepted would show "1"
Upvotes: 1
Reputation: 34765
float currentVersionCode;
try {
currentVersionCode = this.getPackageManager().getPackageInfo(
this.getApplicationInfo().packageName, 0).versionCode;
Toast.makeText(this, ""+currentVersionCode, Toast.LENGTH_SHORT).show();
} catch (NameNotFoundException e) {
return;
}
Try this...a minor change
use the below for your reference...
String versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0 ).versionName;
Upvotes: 2
Reputation: 28519
The problem is that you are passing the version to Toast.makeText as an int. If you pass an int to makeText it assumes it's a String resource ID. This is why you are getting a string resource not found error.
Convert the version code to a String, before passing it to makeText.
Upvotes: 1