开发者

android context nullpointerexception

开发者 https://www.devze.com 2023-04-05 05:12 出处:网络
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 :

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开发者_如何学C 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?


@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.


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();


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


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();


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号