Skip to content Skip to sidebar Skip to footer

Null Pointer Exception In Layoutinflator

I am getting a Null Pointer Exception at LayoutInflator in my ExpandListAdapter class that extends BaseExpandableListAdaptor. import java.util.ArrayList; import android.content.Co

Solution 1:

I am getting exception at the line: LayoutInflater infalInflater = (LayoutInflater)this.context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

That is because context is null. Make sure that you are passing in your Activity to the ExpandListAdapter constructor.

Also, its showing a warning at that line saying "The static field Context.LAYOUT_INFLATER_SERVICE should be accessed in a static way"

Change your constructor to take an Activity rather than a Context, then change:

LayoutInflaterinfalInflater= (LayoutInflater) this.context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

to:

LayoutInflaterinfalInflater= whateverYouDecideToCallYourActivityDataMember.getLayoutInflater();

Solution 2:

So first the easy part. The warning that says you should access LAYOUT_INFLATER_SERVICE in a static way wants you to change it from context.LAYOUT_INFLATER_SERVICE to Context.LAYOUT_INFLATER_SERVICE (capitol 'C') so you are accessing the variable through the class rather than the instance.

Second, the null pointer exception is definitely happening on your 'context' variable, so whenever you are instantiating ExpandListAdapter, you are giving it a null value for context. You didn't give me that code, so I can't look into it, but I would recommend instantiating your ExpandListAdapter in onCreateView or onViewCreated. If you are creating this in an activity, pass it a reference to the activity (i.e. 'this'), and if you are creating it in a fragment pass it a call to getActivity(). This won't work though if your Fragment isn't attached to the activity though (which may be your issue).

Post a Comment for "Null Pointer Exception In Layoutinflator"