android 上下文空指针异常

发布于 2024-12-05 06:03:36 字数 1237 浏览 1 评论 0原文

我对 android Context 有一个小问题,我不知道如何解决这个问题。这是我正在使用的代码:

public class TestActivity {
Context context;
public static  String getPackageVersion(){  
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
            version = packageInfo.versionName;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return version;
    }

    public static boolean checkClientApiVer(String clientApiVer){

        int s = RPCCommunicator.strVerToIntVer(clientApiVer);
        int c = RPCCommunicator.strVerToIntVer(getPackageVersion());

        return (c>=s);
    }

     public boolean execute() {

        serverApiVer = jsonObj.getString("server_api_ver");
        Log.w("SERVER API VER","SHOW SERVER API VERSION : "+serverApiVer);

            checkClientApiVer(serverApiVer);
}

}

它在这一行中显示 Nullpointer 异常:

PackageManager pm = context.getPackageManager();

实际上我不能使用 this.getPackageManager ()TestActivity.getPackageManager() 并且我无法将 context 设置为 this

有什么建议吗?

I have a little issue with android Context and I don't know how to solve the problem.Here is the code I am using :

public class TestActivity {
Context context;
public static  String getPackageVersion(){  
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
            version = packageInfo.versionName;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
        return version;
    }

    public static boolean checkClientApiVer(String clientApiVer){

        int s = RPCCommunicator.strVerToIntVer(clientApiVer);
        int c = RPCCommunicator.strVerToIntVer(getPackageVersion());

        return (c>=s);
    }

     public boolean execute() {

        serverApiVer = jsonObj.getString("server_api_ver");
        Log.w("SERVER API VER","SHOW SERVER API VERSION : "+serverApiVer);

            checkClientApiVer(serverApiVer);
}

}

and it says Nullpointer exception in this line :

PackageManager pm = context.getPackageManager();

Actually I can't use this.getPackageManager(), or TestActivity.getPackageManager() and I can't set context to this.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

失眠症患者 2024-12-12 06:03:37

@Roflcoptr 指出了基本的,但实际上,您的类不扩展活动,因此它不是上下文,请将其更改为:

public class TestActivity extends Activity

如果您希望它是一个实际的活动,或者,如果它应该只是一个辅助类,实例化时将 Activity 传递给它。

@Roflcoptr pointed out the basic, but actually, your class doesn't extend activity, so it is not a context, change it to:

public class TestActivity extends Activity

if you want it to be an actual activity, or, if it should be only a helper class, pass it the activity when it is instantiated.

維他命╮ 2024-12-12 06:03:37

如果你的课程是一项活动,最好将其用作上下文。如果您需要另一个类中的上下文,则可以在 applicationContext 上有一个单例指针。

public class MyApp extends Application {

    private static MyApp instance;

    public MyApp() {
        instance = this;
    }

    public static Context getContext() {
        return instance;
    }
}

在你的清单文件中:

<application
    android:name="com.mycompany.appname.MyApp"
    android:icon="@drawable/icon"
    android:label="@string/app_name">

现在你总是可以有一个上下文

MyApp.GetContext();

If your class is an activity, it's better to use this as a context. If you need a context in another class, you can have a singleton pointer on your applicationContext.

public class MyApp extends Application {

    private static MyApp instance;

    public MyApp() {
        instance = this;
    }

    public static Context getContext() {
        return instance;
    }
}

and in your manifest file :

<application
    android:name="com.mycompany.appname.MyApp"
    android:icon="@drawable/icon"
    android:label="@string/app_name">

Now you can always have a context with

MyApp.GetContext();
关于从前 2024-12-12 06:03:37

您应该通过添加不是 Activity 的类的构造函数来初始化上下文,

public TestActivity(Context c) {
   this.context = c;
}

并在 Activity 中通过发送 this 作为参数来实例化 TestActivity ,如下所示:

TestActivity tActivity = new TestActivity(this);//this refer to the Activity

第二种解决方案是扩展 Activity ,您应该覆盖方法onCreate()

you should to initialize your context by adding a constructor of your class which is not an Activity ,

public TestActivity(Context c) {
   this.context = c;
}

and in your Activity , instantiate the TestActivity by sending this as a parameter like this :

TestActivity tActivity = new TestActivity(this);//this refer to the Activity

The second solution is by extending an Activity , and you should override the method onCreate()

阳光的暖冬 2024-12-12 06:03:37

问题是您声明了上下文,但从未实例化或分配对其的引用。所以它指向null。

通常,您的 TestActivity 应该是 Activity 类或类似类的子类。

在这种情况下你可以做类似的事情

this.getPackageManager();

The problem is that you declared context but never instantiated or assigned a reference to it. So it points to null.

Normally you TestActivity should be a subclass from the Activity class or something similar.

In this case you could do something like

this.getPackageManager();
久随 2024-12-12 06:03:37

您没有创建特定类的任何对象,而不是在类中创建上下文对象并保留活动或应用程序的引用,这会导致 android bcz 中的内存泄漏。

您可以通过执行以下步骤来实现此目的

1>创建应用程序上下文的单例类并在manifest.xml中定义应用程序类;因此它们将是整个应用程序生命周期中的一个上下文对象

2>当您使用该方法时传递上下文。

public static  String getPackageVersion(Context context){  
    try {
        PackageManager pm = context.getPackageManager();
        PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
        version = packageInfo.versionName;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    return version;
}  

instead of creating context object in class and keeping reference of activity or application lead to memory leaks in android bcz you are not creating any object of particular class.

you can achieve this by doing below steps

1> create singleton class of application context and define application class in manifest.xml ; so their will be one context object in entire application life cycle

2> pass context when ever you are using that method.

public static  String getPackageVersion(Context context){  
    try {
        PackageManager pm = context.getPackageManager();
        PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
        version = packageInfo.versionName;
    } catch (NameNotFoundException e) {
        e.printStackTrace();
    }
    return version;
}  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文