试图获取版本号但应用程序崩溃

时间:2012-04-05 16:07:41

标签: android eclipse version crash-reports

我想获取应用程序的版本号,但是在我尝试时应用程序会在加载时崩溃。

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'

如何解决问题?

提前致谢。

3 个答案:

答案 0 :(得分:2)

float currentVersionCode;
try {
    currentVersionCode = this.getPackageManager().getPackageInfo(
            this.getApplicationInfo().packageName, 0).versionCode;
    Toast.makeText(this, ""+currentVersionCode, Toast.LENGTH_SHORT).show();
} catch (NameNotFoundException e) {
    return;
}

试试这个......一个小小的改变

使用以下内容作为参考......

String versionName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0 ).versionName;

答案 1 :(得分:1)

问题是您将版本作为int传递给Toast.makeText。如果将int传递给makeText,则假定它是String资源ID。这就是为什么你得到一个字符串资源未找到错误。

在将版本代码传递给makeText之前,将其转换为String。

答案 2 :(得分:1)

如果你想要versionName,那么:

   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();
    }

更合适,只要你有

就行
  android:versionCode="1"
  android:versionName="Mickey Mouse" 

它会显示“米老鼠”。你接受的答案会显示“1”