1

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

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

日志猫:

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'

我该如何解决我的问题?

提前致谢。

4

3 回答 3

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;
于 2012-04-05T16:23:09.450 回答
1

问题是您将版本作为 int 传递给 Toast.makeText。如果您将一个 int 传递给 makeText,它假定它是一个字符串资源 ID。这就是您收到未找到字符串资源错误的原因。

将版本代码转换为字符串,然后将其传递给 makeText。

于 2012-04-05T16:10:18.457 回答
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”

于 2012-04-05T16:33:11.797 回答