Android LayoutFlator中出现空指针异常

Android LayoutFlator中出现空指针异常,android,android-listview,android-adapter,android-adapterview,Android,Android Listview,Android Adapter,Android Adapterview,我在扩展BaseExpandableListAdapter的ExpandListAdapter类中的LayoutFlator处遇到空指针异常 import java.util.ArrayList; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Bas

我在扩展BaseExpandableListAdapter的ExpandListAdapter类中的LayoutFlator处遇到空指针异常

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;
        }
}
import java.util.ArrayList;
导入android.content.Context;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.BaseExpandableListAdapter;
导入android.widget.TextView;
公共类ExpandListAdapter扩展了BaseExpandableListAdapter{
私人语境;
私有ArrayList组;
公共ExpandListAdapter(上下文、ArrayList组){
this.context=context;
这个。组=组;
}
公共无效附加项(ExpandListChild项,ExpandListGroup组){
如果(!groups.contains(组)){
组。添加(组);
}
int index=组。indexOf(组);
ArrayList ch=groups.get(index.getItems();
总附录(项目);
groups.get(index).setItems(ch);
}
公共对象getChild(int-groupPosition,int-childPosition){
ArrayList chList=groups.get(groupPosition.getItems();
返回chList.get(childPosition);
}
公共长getChildId(int-groupPosition,int-childPosition){
返回子位置;
}
公共视图getChildView(int-groupPosition、int-childPosition、boolean-isLastChild、视图视图、视图组父级){
ExpandListChild=(ExpandListChild)getChild(groupPosition,childPosition);
如果(视图==null){
LayoutInflater INFLATER=(LayoutInflater)this.context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
视图=infalInflater.充气(R.layout.expandlist\u child\u项,空);
}
TextView tv=(TextView)view.findviewbyd(R.id.tvChild);
tv.setText(child.getName().toString());
tv.setTag(child.getTag());
//TODO自动生成的方法存根
返回视图;
}
公共整数getChildrenCount(整数组位置){
ArrayList chList=groups.get(groupPosition.getItems();
返回chList.size();
}
公共对象getGroup(int-groupPosition){
返回groups.get(groupPosition);
}
public int getGroupCount(){
返回组。size();
}
公共长getGroupId(int-groupPosition){
//TODO自动生成的方法存根
返回组位置;
}
公共视图getGroupView(int-groupPosition、布尔值isLastChild、视图视图、视图组父级){
ExpandListGroup=(ExpandListGroup)getGroup(groupPosition);
如果(视图==null){
LayoutInflater inf=(LayoutInflater)context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
view=inf.inflate(R.layout.two\u lines\u list\u layout,null);
}
TextView tv=(TextView)view.findViewById(R.id.line\u book);
tv.setText(group.getName());
TextView tv2=(TextView)view.findViewById(R.id.line_author);
tv2.setText(group.getAuthor());
返回视图;
}
公共布尔表ID(){
//TODO自动生成的方法存根
返回true;
}
公共布尔值isChildSelectable(int arg0,int arg1){
//TODO自动生成的方法存根
返回true;
}
}
我在以下行遇到异常:LayoutInflater infalInflater=(LayoutInflater)this.context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE)

此外,它在该行显示一条警告,表示“应以静态方式访问static field Context.LAYOUT\u充气机\u服务”

请帮忙

我在以下行遇到异常:LayoutInflater infalInflater=(LayoutInflater)this.context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE)

这是因为
context
null
。确保您正在将
活动
传递给
ExpandListAdapter
构造函数

此外,它在该行显示一条警告,表示“应以静态方式访问static field Context.LAYOUT\u充气机\u服务”

将构造函数更改为采用
活动
而不是
上下文
,然后更改:

LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
致:

我在以下行遇到异常:LayoutInflater infalInflater=(LayoutInflater)this.context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE)

这是因为
context
null
。确保您正在将
活动
传递给
ExpandListAdapter
构造函数

此外,它在该行显示一条警告,表示“应以静态方式访问static field Context.LAYOUT\u充气机\u服务”

将构造函数更改为采用
活动
而不是
上下文
,然后更改:

LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
致:


首先是简单的部分。警告说您应该以静态方式访问LAYOUT\u INFLATER\u服务,希望您将其从context.LAYOUT\u INFLATER\u服务更改为context.LAYOUT\u INFLATER\u服务(capitol'C'),因此您是通过类而不是实例访问变量


第二,空指针异常肯定发生在“context”变量上,因此无论何时实例化ExpandListAdapter,都会为context指定一个空值。您没有给我那个代码,所以我无法查看它,但我建议您在onCreateView或onViewCreated中实例化ExpandListAdapter。如果您在活动中创建它,则向其传递对活动的引用(即“this”),如果您在片段中创建它,则向其传递对getActivity()的调用。但是,如果你的片段没有附加到活动上,这将不起作用(这可能是你的问题)。

因此,首先是简单的部分。警告说您应该访问布局充气机