PackageInfo versionCode和versionName null在手机上,但适用于模拟器

时间:2011-07-18 05:25:12

标签: android-emulator android

我正在构建一个不会通过市场的私人应用。因此,我需要提取当前版本的应用程序(而不是平台)信息,并检查服务器以查看是否有更新。它在模拟器上运行良好,但是当我在我的Droid X上安装已签名的APK时,它返回null。这是代码:

PackageInfo pInfo = null;

try {
    pInfo = getPackageManager().getPackageInfo("com.rubiconproject.expenses",
                PackageManager.GET_META_DATA);
} catch (NameNotFoundException e) {
    return;
} 
String postUrl = DbAdapter.REMOTE_DB +
                "/application/active-version/platform/android/version/" +
                pInfo.versionCode + "." + pInfo.versionName;

编辑:

我在下面写了一个小得多的应用程序来显示我的问题(TestingActivity.java):

package com.rubiconproject.testing;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.widget.TextView;

public class TestingActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        PackageInfo pInfo = null;
        try {
            pInfo = getPackageManager().getPackageInfo(
                "com.rubiconproject.testing", PackageManager.GET_META_DATA);
        } catch (NameNotFoundException e) {
            return;
        }
        TextView tt = (TextView)findViewById(R.id.textView1);
        tt.setText(pInfo.versionCode);
        TextView tt2 = (TextView)findViewById(R.id.textView2);
        tt2.setText(pInfo.versionName);
    }
}

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
        package="com.rubiconproject.testing"
        android:versionCode="1"
        android:versionName="1.0">
    <uses-sdk android:minSdkVersion="9" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".TestingActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

adb logcat显示:

E/AndroidRuntime(  335): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rubiconproject.testing/com.rubiconproject.testing.TestingActivity}: java.lang.NullPointerException
E/AndroidRuntime(  335):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
E/AndroidRuntime(  335):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
E/AndroidRuntime(  335):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime(  335):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
E/AndroidRuntime(  335):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  335):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  335):    at android.app.ActivityThread.main(ActivityThread.java:3647)
E/AndroidRuntime(  335):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  335):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(  335):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime(  335):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime(  335):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(  335): Caused by: java.lang.NullPointerException
E/AndroidRuntime(  335):    at com.rubiconproject.testing.TestingActivity.onCreate(TestingActivity.java:26)
E/AndroidRuntime(  335):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(  335):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1586)
E/AndroidRuntime(  335):    ... 11 more

第26行对应于:

    tt.setText(pInfo.versionCode);

有谁知道为什么会这样?

2 个答案:

答案 0 :(得分:2)

您的manifest.xml是否包含android:versionCode和androd:versionName属性?

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.package.name"
  android:versionCode="2"
  android:versionName="1.1">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        ...
    </application>
</manifest>

答案 1 :(得分:0)

想出来。问题出在键盘和椅子之间。我看到null,当事实上字符串构建不正确。