PackageInfo版本code和手机上VERSIONNAME空,但它只是对模拟器模拟器、但它、机上、版本

2023-09-05 08:20:31 作者:光头强我给你树你教我勇敢

我建立一个专用的应用程序,不会通过卖场。这样一来,我需要拉的当前版本的应用程序(而不是平台)信息,并检查服务器,查看是否有更新。它的伟大工程在模拟器上,但是当我安装APK签上我的Droid X,它返回null。这里是code:

I am building a private app that will not go through the Marketplace. As a result, I need to pull the current version application (not platform) information and check with the server to see if there is an update. It works great on the emulator, but when I installed the signed APK on my Droid X, it returns null. Here is the code:

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):

I wrote a much smaller application, below, that displays my problem (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>

亚行logcat显示:

adb logcat shows:

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对应于:

where line 26 corresponds to:

    tt.setText(pInfo.versionCode);

有谁知道为什么会这样?

Does anyone know why this is happening?

推荐答案

请问您的manifest.xml包含安卓版code和androd:VERSIONNAME属性

Does your manifest.xml contain android:versionCode and androd:versionName attributes?

<?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>