【问题标题】:Null Pointer Exception in LayoutInflatorLayoutInflator 中的空指针异常
【发布时间】:2013-07-19 23:41:06
【问题描述】:

我在扩展 BaseExpandableListAdaptor 的 ExpandListAdapter 类中的 LayoutInflator 处遇到空指针异常。

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);

此外,它在该行显示一条警告“静态字段 Context.LAYOUT_INFLATER_SERVICE 应以静态方式访问”

请帮忙..!!

【问题讨论】:

    标签: android android-listview android-adapter android-adapterview


    【解决方案1】:

    首先是简单的部分。说您应该以静态方式访问 LAYOUT_INFLATER_SERVICE 的警告希望您将其从 context.LAYOUT_INFLATER_SERVICE 更改为 Context.LAYOUT_INFLATER_SERVICE(大写字母“C”),以便您通过类而不是实例访问变量。

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

    【讨论】:

    • 非常感谢,确实在实例化 ExpandListAdapter 时我传递了一个空上下文。我是从一个片段中调用它,所以 getActivity() 工作......!
    【解决方案2】:

    我在以下行遇到异常:LayoutInflater infalInflater = (LayoutInflater)this.context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

    那是因为contextnull。确保将 Activity 传递给 ExpandListAdapter 构造函数。

    此外,它在该行显示一条警告“静态字段 Context.LAYOUT_INFLATER_SERVICE 应以静态方式访问”

    将您的构造函数更改为采用Activity 而不是Context,然后更改:

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

    到:

    LayoutInflater infalInflater = whateverYouDecideToCallYourActivityDataMember.getLayoutInflater();
    

    【讨论】:

    • 感谢您的帮助。我没有将 Activity 传递给 ExpandListAdapter 构造函数,而是传递了一个空上下文。
    猜你喜欢
    • 1970-01-01
    • 2013-12-19
    • 2015-07-12
    相关资源
    最近更新 更多