2012-04-05 68 views
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; 
} 

的logcat:

04-05 16:01:43.108: D/LocationManager(1109): Constructor: service = [email protected] 
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' 

我怎樣才能解決我的問題?

在此先感謝。

回答

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; 
+0

你會得到1.3而不是1 – 2012-04-05 16:24:00

+0

嗯。多麼奇怪。它打印1.0:S Woa!等待! 'android:versionName'在1.3上,而不是'android:versionCode'。謝謝! – Erik 2012-04-05 16:24:58

1

問題是,您將該版本作爲int傳遞給Toast.makeText。如果你將一個int傳遞給makeText,它會假定它是一個String資源ID。這就是爲什麼你得到一個字符串資源找不到錯誤。

將版本代碼轉換爲字符串,然後將其傳遞給makeText。

+0

謝謝,但我得到'類型不匹配:當我更改'int currentVersionCode;'無法從int轉換爲字符串''字符串currentVersionCode;' – Erik 2012-04-05 16:14:14

+0

Toast.makeText(this, 「」+ currentVersionCode,Toast.LENGTH_SHORT).show(); – 2012-04-05 16:16:40

+0

@Agarwal:謝謝,但它只打印'1'。我在我的'AndroidManifest.xml'文件中:'android:versionName =「1.3」'我也想打印 – Erik 2012-04-05 16:19:53

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」

相關問題