I am developing an android application. My question is can I get the application context in an abstract class? it gives me null pointer exception for the context variable and I assume it is maybe because I am using it inside an abstract class.
NOTE: In my code I have an abstract class (the abstract class itself extends Activity) that I use for instantiating other classes – Activites. In abstract class i create an instance of an activity helper, a class that handles some common activity-related functionality in the app. The is my activity helper class:
public class ActivityHelper {
protected Activity mActivity;
public static ActivityHelper createInstance(Activity activity, Context context) {
return UIUtils.isTablet(context) ?
new ActivityHelperTablet(activity) :
new ActivityHelper(activity);
}
protected ActivityHelper(Activity activity) {
mActivity = activity;
}
}
Where the function isTablet receives a context variable that I create in my abstract class
public static boolean isTablet(Context context) {
// Can use static final constants like HONEYCOMB, declared in later versions
// of the OS since they are inlined at compile time.
return isHoneycomb() ||
((context.getResources().getConfiguration().screenLayout &
Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE);
}
the important part of BaseActivity:
public abstract class BaseActivity extends Activity {
Context context = this.getApplicationContext();
final ActivityHelper mActivityHelper = ActivityHelper.createInstance(this, context);
}
But it gives me a null pointer exception. So I thought that it’s maybe because of the abstract class.
And this is the log:
06-12 15:59:56.011: E/AndroidRuntime(1206): Caused by: java.lang.NullPointerException
06-12 15:59:56.011: E/AndroidRuntime(1206): at
android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
06-12 15:59:56.011: E/AndroidRuntime(1206): at
com.navayo.sec.voip.activities.BaseActivity.<init>(BaseActivity.java:35)
06-12 15:59:56.011: E/AndroidRuntime(1206): at
com.navayo.sec.voip.activities.MainActivity.<init>(MainActivity.java:43)
06-12 15:59:56.011: E/AndroidRuntime(1206): at
java.lang.Class.newInstanceImpl(Native Method)
06-12 15:59:56.011: E/AndroidRuntime(1206): at
java.lang.Class.newInstance(Class.java:1319)
06-12 15:59:56.011: E/AndroidRuntime(1206): at
android.app.Instrumentation.newActivity(Instrumentation.java:1023)
06-12 15:59:56.011: E/AndroidRuntime(1206): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
Having an abstact class doesn’t matter.
You have a null pointer because your context is null, which means somewhere in your methods there is an error, without the code to review i can’t help you more.
EDIT:
this should be enough. tell me if this is helping. simply an activity extends from context so you even might do:
UIUtils.isTablet(activity);now another subject would be the way you are actually creating the instances which is impossible to tell from your code, Activities are started with intents and have a lifecycle, they cannot be constructed with constructors.
EDIT 2:
Your issue is in this code:
At this time the activity is not yet created so your context (“this”) is null.
you need to move the activity helper initialization in onCreate. the activity works a bit differently from other classes, it might be a good idea to also read a bit more on the activity lifecycle and see some more examples (try google :)).
the correct case would be: