在LayoutInflator空指针异常 [英] Null Pointer Exception in LayoutInflator
问题描述
我得到一个空指针异常的LayoutInflator在扩展BaseExpandableListAdaptor我ExpandListAdapter类。
I am getting a Null Pointer Exception at LayoutInflator in my ExpandListAdapter class that extends BaseExpandableListAdaptor.
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class ExpandListAdapter extends BaseExpandableListAdapter{
private Context context;
private ArrayList<ExpandListGroup> groups;
public ExpandListAdapter(Context context, ArrayList<ExpandListGroup> groups){
this.context=context;
this.groups=groups;
}
public void addItem(ExpandListChild item, ExpandListGroup group){
if(!groups.contains(group)){
groups.add(group);
}
int index=groups.indexOf(group);
ArrayList<ExpandListChild> ch=groups.get(index).getItems();
ch.add(item);
groups.get(index).setItems(ch);
}
public Object getChild(int groupPosition, int childPosition){
ArrayList<ExpandListChild> chList=groups.get(groupPosition).getItems();
return chList.get(childPosition);
}
public long getChildId(int groupPosition, int childPosition){
return childPosition;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View view, ViewGroup parent){
ExpandListChild child = (ExpandListChild) getChild(groupPosition, childPosition);
if (view == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
view = infalInflater.inflate(R.layout.expandlist_child_item, null);
}
TextView tv = (TextView) view.findViewById(R.id.tvChild);
tv.setText(child.getName().toString());
tv.setTag(child.getTag());
// TODO Auto-generated method stub
return view;
}
public int getChildrenCount(int groupPosition) {
ArrayList<ExpandListChild> chList = groups.get(groupPosition).getItems();
return chList.size();
}
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
public int getGroupCount() {
return groups.size();
}
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isLastChild, View view, ViewGroup parent) {
ExpandListGroup group = (ExpandListGroup) getGroup(groupPosition);
if (view == null) {
LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
view = inf.inflate(R.layout.two_lines_list_layout, null);
}
TextView tv = (TextView) view.findViewById(R.id.line_book);
tv.setText(group.getName());
TextView tv2 = (TextView) view.findViewById(R.id.line_author);
tv2.setText(group.getAuthor());
return view;
}
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
public boolean isChildSelectable(int arg0, int arg1) {
// TODO Auto-generated method stub
return true;
}
}
我得到的异常在该行:LayoutInflater infalInflater =(LayoutInflater)this.context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
I am getting exception at the line: LayoutInflater infalInflater = (LayoutInflater)this.context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
此外,它显示出在该行警告称静态字段Context.LAYOUT_INFLATER_SERVICE应以静态的方式来访问
Also, its showing a warning at that line saying "The static field Context.LAYOUT_INFLATER_SERVICE should be accessed in a static way"
请帮助.. !!
推荐答案
因此,首先最容易的部分。说你应该以静态的方式访问LAYOUT_INFLATER_SERVICE警告要你从context.LAYOUT_INFLATER_SERVICE改为Context.LAYOUT_INFLATER_SERVICE(国会'C'),所以你通过类访问变量,而不是实例。
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.
二,空指针异常肯定是发生在你的语境变量,所以每当你实例ExpandListAdapter,你给它的上下文空值。你没有给我,code,所以我不能看进去,但我会建议在实例或onCreateView您onViewCreated ExpandListAdapter。如果你是在一个活动创造这一点,把它传递给活动的引用(即本),如果你是在一个片段创建它把它传递给getActivity()的调用。如果你的碎片没有连接到该活动虽然这不会工作,虽然(它可能是你的问题)。
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).
这篇关于在LayoutInflator空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!