0

我有一个导入 VersionCheck.java 的 LoginActivity.java。

VersionCheck.java 有一个类 VersionCheck,它扩展了 Activity 以获得对 getPackageManager() 的访问。

在 LoginActivity 中,我得到一个 VersionCheck 类的对象,并运行 getVersionName 方法来检索应用程序的 versionName。

当我运行应用程序时,我在 getVersionName 方法中得到一个java.lang.NullPointerException pinfo = getPackageManager().getPackageInfo(packageName, 0);

如果我将 getVersionName 方法放在我的主类中,代码就可以工作,但是我想用这个方法创建一个单独的类,因为我在应用程序中经常使用这个方法。

请参阅下面的代码。

登录活动.java

package com.itse.htsurvey;

import com.itse.tools.*;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class LoginActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        displayVersionName();           

    }

    //---------------------------------------------------------------------------------------------------------
    //retrieves version name and displays it
    //---------------------------------------------------------------------------------------------------------
    private void displayVersionName() {

        //object of class versioncheck
        VersionCheck vc = new VersionCheck();

        TextView tvVersion = (TextView) findViewById(R.id.tvVersion);
        tvVersion.setText("Version " + vc.getVersionName(getPackageName()));
    }


}

版本检查.java

package com.itse.tools;

import android.app.Activity;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;

public class VersionCheck extends Activity{

    //---------------------------------------------------------------------------------------------------------
    //Retrieves the version name of the application and returns to method caller
    //---------------------------------------------------------------------------------------------------------
    public String getVersionName(String packageName) {

        PackageInfo pinfo = null;
        try {
            pinfo = getPackageManager().getPackageInfo(packageName, 0);
        } catch (NameNotFoundException e) {         
            e.printStackTrace();
        }

        return pinfo.versionName;    
    }

}

错误信息

05-24 13:07:23.146: W/dalvikvm(888): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
05-24 13:07:23.166: E/AndroidRuntime(888): FATAL EXCEPTION: main
05-24 13:07:23.166: E/AndroidRuntime(888): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.itse.htsurvey/com.itse.htsurvey.LoginActivity}: java.lang.NullPointerException
05-24 13:07:23.166: E/AndroidRuntime(888):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-24 13:07:23.166: E/AndroidRuntime(888):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-24 13:07:23.166: E/AndroidRuntime(888):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-24 13:07:23.166: E/AndroidRuntime(888):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-24 13:07:23.166: E/AndroidRuntime(888):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-24 13:07:23.166: E/AndroidRuntime(888):  at android.os.Looper.loop(Looper.java:137)
05-24 13:07:23.166: E/AndroidRuntime(888):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-24 13:07:23.166: E/AndroidRuntime(888):  at java.lang.reflect.Method.invokeNative(Native Method)
05-24 13:07:23.166: E/AndroidRuntime(888):  at java.lang.reflect.Method.invoke(Method.java:511)
05-24 13:07:23.166: E/AndroidRuntime(888):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-24 13:07:23.166: E/AndroidRuntime(888):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-24 13:07:23.166: E/AndroidRuntime(888):  at dalvik.system.NativeStart.main(Native Method)
05-24 13:07:23.166: E/AndroidRuntime(888): Caused by: java.lang.NullPointerException
05-24 13:07:23.166: E/AndroidRuntime(888):  at android.content.ContextWrapper.getPackageManager(ContextWrapper.java:86)
05-24 13:07:23.166: E/AndroidRuntime(888):  at com.itse.tools.VersionCheck.getVersionName(VersionCheck.java:16)
05-24 13:07:23.166: E/AndroidRuntime(888):  at com.itse.htsurvey.LoginActivity.displayVersionName(LoginActivity.java:29)
05-24 13:07:23.166: E/AndroidRuntime(888):  at com.itse.htsurvey.LoginActivity.onCreate(LoginActivity.java:16)
05-24 13:07:23.166: E/AndroidRuntime(888):  at android.app.Activity.performCreate(Activity.java:4465)
05-24 13:07:23.166: E/AndroidRuntime(888):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-24 13:07:23.166: E/AndroidRuntime(888):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-24 13:07:23.166: E/AndroidRuntime(888):  ... 11 more
4

1 回答 1

1

您不能只扩展 Activity 类。您应该将第一个活动的上下文传递给该方法。

喜欢:

public static String getVersionName(Context context, String packageName) {

    PackageInfo pinfo = null;
    try {
        pinfo = context.getPackageManager().getPackageInfo(packageName, 0);
    } catch (NameNotFoundException e) {         
        e.printStackTrace();
        // Can not return pinfo.versionName since pinfo wasn't assigned due to exception
        return null;
    }

    return pinfo.versionName;    
}

如果您将其用作活动,则仅扩展 Activity 类:D

于 2012-05-24T11:55:02.887 回答