Android context nullpointerexception

I have a little problem with android Context and I don't know how to solve the problem. Here is the code I'm 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 this line indicates the Nullpointer exception:

 PackageManager pm = context.getPackageManager(); 

Actually, I cannot use this.getPackageManager() or TestActivity.getPackageManager() , and I cannot set context to this .

Any suggestions?

+3
source share
5 answers

@Roflcoptr pointed out the main one, but in fact your class does not expand activity, so it is not a context and changes it to:

 public class TestActivity extends Activity 

if you want this to be an actual action, or if it should only be a helper class, pass the action to it when you create it.

+3
source

If your class is active, it is best to use it as a context. If you need a context in another class, you can have a single point pointer to 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 context with

 MyApp.GetContext(); 
+3
source

The problem is that you declared the context, but you never created or referenced it. Therefore, it points to null.

Usually you TestActivity should be a subclass of the Activity class or something similar.

In this case, you can do something like

 this.getPackageManager(); 
+2
source

you must initialize your context by adding the constructor of your class, which is not an Activity,

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

and in your activity, create an instance of TestActivity by sending this as a parameter like this:

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

The second solution is to extend the action, and you must override the onCreate() method

+2
source

instead of creating a context object in the class and saving the link to the activity or the application leads to memory leaks in android bcz, you are not creating an object of a specific class.

you can achieve this by following these steps

1> create a singleton application context class and define the application class in manifest.xml; therefore, they will be one contextual object throughout the life cycle of the application.

2> pass context when you use this 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; } 
+1
source

Source: https://habr.com/ru/post/1496991/


All Articles