在android中使用自定义ExpandableListview时出现异常

时间:2014-08-06 12:47:03

标签: android expandablelistview

在getGroupView()中遇到异常请帮我解决这个问题,在v = inflater.inflate(R.layout.view_group,null)

的getGroupView方法中显示NullPointerException
public ExpandableDrawerAdapter(Context c) 
{
 this.con=c;

}

@Override
public Object getChild(int groupPosition, int childPosition) 
{
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getChildId(int groupPosition, int childPosition) 
{
    // TODO Auto-generated method stub
    return childPosition;
}

@Override
public View getChildView(int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) 
{

    View v1=null;
    LayoutInflater inflater=(LayoutInflater) con.getSystemService("LAYOUT_INFLATER_SERVICE");
    v1=inflater.inflate(R.layout.view_child, null);
    TextView tvChildName=(TextView)v1.findViewById(R.id.textView2);
    tvChildName.setText(child[groupPosition][childPosition]);
    return v1;

}

@Override
public int getChildrenCount(int groupPosition) 
{
    // TODO Auto-generated method stub
    return child[groupPosition].length;
}

@Override
public Object getGroup(int groupPosition) 
{
    // TODO Auto-generated method stub
    return null;
}

@Override
public int getGroupCount() 
{
    // TODO Auto-generated method stub
    return parent.length;
}

@Override
public long getGroupId(int groupPosition) 
{
    // TODO Auto-generated method stub
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup p) 
{

    View v=null;
    LayoutInflater inflater=(LayoutInflater) con.getSystemService("LAYOUT_INFLATER_SERVICE");
    v=inflater.inflate(R.layout.view_group, null);
    if (isExpanded) 
    {
        v.setBackgroundColor(Color.parseColor("#252525"));
    }

    ImageView groupImage = (ImageView) v
            .findViewById(R.id.imageView1);

    ImageView ivGroupArrow = (ImageView) v
            .findViewById(R.id.imageView2);

    TextView tvGroupName = (TextView) v
            .findViewById(R.id.textView1);
    tvGroupName.setText(parent[groupPosition]);

    ivGroupArrow.setVisibility(View.INVISIBLE);
    groupImage.setVisibility(View.VISIBLE);


    return v;

}

@Override
public boolean hasStableIds() 
{
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) 
{
    // TODO Auto-generated method stub
    return true;
}

}

这是我的Logcat

08-06 08:39:51.760: E/AndroidRuntime(24009): FATAL EXCEPTION: main
08-06 08:39:51.760: E/AndroidRuntime(24009): Process: com.main.drawer, PID: 24009
08-06 08:39:51.760: E/AndroidRuntime(24009): java.lang.NullPointerException
08-06 08:39:51.760: E/AndroidRuntime(24009):    at com.main.adapter.ExpandableDrawerAdapter.getGroupView(ExpandableDrawerAdapter.java:97)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:446)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at android.widget.AbsListView.obtainView(AbsListView.java:2263)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at android.widget.ListView.makeAndAddView(ListView.java:1790)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at android.widget.ListView.fillDown(ListView.java:691)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at android.widget.ListView.fillFromTop(ListView.java:752)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at android.widget.ListView.layoutChildren(ListView.java:1630)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at android.widget.AbsListView.onLayout(AbsListView.java:2091)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at android.view.View.layout(View.java:14817)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:714)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at android.view.View.layout(View.java:14817)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at android.view.View.layout(View.java:14817)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:374)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at android.view.View.layout(View.java:14817)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at android.view.View.layout(View.java:14817)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at android.view.ViewGroup.layout(ViewGroup.java:4631)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
08-06 08:39:51.760: E/AndroidRuntime(24009):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)

1 个答案:

答案 0 :(得分:1)

您使用了错误的名称来获取布局的正确值为Context.LAYOUT_INFLATER_SERVICE