Expandablelistview无法在android中的片段活动中工作

时间:2013-10-16 06:39:09

标签: android nullpointerexception fragment expandablelistview layout-inflater

我在片段中使用可扩展的listview。但是它没有为LayoutInflater获取空指针异常。

    public class DemoActivity extends Fragment{
public static final String ARG_SECTION_NUMBER = "section_number";
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
Context mContext;

public DemoActivity(Context context) {
    this.mContext=DemoActivity.this.getActivity();;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.demo, container, false);
    TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label_demo);
    dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
    // get the listview
    expListView = (ExpandableListView) rootView.findViewById(R.id.lvExp);

    // preparing list data
    prepareListData();

    listAdapter = new ExpandableListAdapter(mContext, listDataHeader, listDataChild);

    // setting list adapter
    expListView.setAdapter(listAdapter);

    // Listview Group click listener
    expListView.setOnGroupClickListener(new OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                int groupPosition, long id) {

            return false;
        }
    });

    // Listview Group expanded listener
    expListView.setOnGroupExpandListener(new OnGroupExpandListener() {

        @Override
        public void onGroupExpand(int groupPosition) {
            Toast.makeText(mContext,
                    listDataHeader.get(groupPosition) + " Expanded",
                    Toast.LENGTH_SHORT).show();
        }
    });

    // Listview Group collasped listener
    expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {

        @Override
        public void onGroupCollapse(int groupPosition) {
            Toast.makeText(mContext,listDataHeader.get(groupPosition) + " Collapsed",
                    Toast.LENGTH_SHORT).show();

        }
    });

    // Listview on child click listener
    expListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                int groupPosition, int childPosition, long id) {
            // TODO Auto-generated method stub
            Toast.makeText(mContext,listDataHeader.get(groupPosition)+ " : "+ listDataChild.get(
                            listDataHeader.get(groupPosition)).get(childPosition), Toast.LENGTH_SHORT).show();
            return false;
        }
    });
    return rootView;
}

private void prepareListData() {
    listDataHeader = new ArrayList<String>();
    listDataChild = new HashMap<String, List<String>>();


    // Adding child data
    List<String> list1 = new ArrayList<String>();
    list1.add("The Shawshank Redemption");

    List<String> nowShowing = new ArrayList<String>();

    nowShowing.add("Despicable Me 2");


    List<String> comingSoon = new ArrayList<String>();
    comingSoon.add("2 Guns");

    String[] data=search();
    // Adding child data
    for(int i=0;i<search().length;i++)
    {
        listDataHeader.add(""+data[i]);
        //Toast.makeText(mContext,data[i], Toast.LENGTH_SHORT).show();


        listDataChild.put(listDataHeader.get(i), list1);

    }
}

public String[] search()
{
    String[] filenames;
    File file = new File(Environment.getExternalStorageDirectory(), "/Recording");
    if(!file.exists()){
        file.mkdirs();
    }

    //filenames = new String[30];
    if(!file.list().equals(""))
    {
        filenames = file.list();
        int total = 0;

        for (int i = 0; i< filenames.length; i++)
        {
            if (filenames[i].contains(".3gp"))
            {
                total++;
            }
        }
    }
    else
    {
        String[] filearray=new String[]{""};
        filenames=filearray;
    }
    return filenames;
}

  }

适配器类 -

   public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<String> _listDataHeader; 
private HashMap<String, List<String>> _listDataChild;
private EditText editTask;
private CheckBox mCB;
private ImageView mImageView;


public ExpandableListAdapter(Context context, List<String> listDataHeader,
        HashMap<String, List<String>> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;

}

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(_listDataHeader.get(groupPosition))
            .get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getChildView(final int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, final ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item, null);
    }

    ImageView txtListChild = (ImageView) convertView.findViewById(R.id.lblListItem);
    txtListChild.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            String data[]=search();
            Intent i=new Intent(_context,DBRoulette.class);
            i.putExtra("file_name", data[groupPosition]);
            _context.startActivity(i);

        }
    });

    ImageView send = (ImageView) convertView.findViewById(R.id.send);
    send.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String data[]=search();
            Intent i=new Intent(_context,XPlayer.class);
            i.putExtra("file_name", data[groupPosition]);
            _context.startActivity(i);
        }
    });


    mImageView=(ImageView) convertView.findViewById(R.id.editImage);
    mImageView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            String data[]=search();
            Intent i=new Intent(_context,GoogleDrive.class);
            i.putExtra("file_name", data[groupPosition]);
            _context.startActivity(i);

        }
    });


    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return _listDataChild.get(_listDataHeader.get(groupPosition)).size();
}

@Override
public Object getGroup(int groupPosition) {
    return _listDataHeader.get(groupPosition);
}

@Override
public int getGroupCount() {
    return _listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    String headerTitle = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater)_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             /***HERE I'M GETTING NULL POINTER EXCEPTION***/

        convertView = infalInflater.inflate(R.layout.list_group, null);
    }
    TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
    lblListHeader.setText(headerTitle);
    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

 public String[] search()
    {
     String[] filenames;
        File file = new File(Environment.getExternalStorageDirectory(), "/Recording");
          if(!file.exists()){
              file.mkdirs();
          }

        //filenames = new String[30];
        if(!file.list().equals(null))
        {
        filenames = file.list();
        int total = 0;

        for (int i = 0; i< filenames.length; i++)
        {
          if (filenames[i].contains(".3gp"))
            {
              total++;
             }
          }
        }
        else
        {
            String[] filearray=new String[]{""};
            filenames=filearray;
        }
        return filenames;
    }
  }

更新了logcat -

 10-16 15:15:44.011: E/AndroidRuntime(731): FATAL EXCEPTION: main
 10-16 15:15:44.011: E/AndroidRuntime(731): java.lang.NullPointerException
 10-16 15:15:44.011: E/AndroidRuntime(731):     at com.example.android.recorder.ExpandableListAdapter.getGroupView(ExpandableListAdapter.java:127)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:445)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.widget.AbsListView.obtainView(AbsListView.java:1315)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.widget.ListView.makeAndAddView(ListView.java:1727)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.widget.ListView.fillDown(ListView.java:652)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.widget.ListView.fillFromTop(ListView.java:709)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.widget.ListView.layoutChildren(ListView.java:1580)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.widget.AbsListView.onLayout(AbsListView.java:1147)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.view.View.layout(View.java:7035)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.view.View.layout(View.java:7035)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.view.View.layout(View.java:7035)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1589)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.view.View.layout(View.java:7035)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.view.View.layout(View.java:7035)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.view.View.layout(View.java:7035)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.view.View.layout(View.java:7035)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at  android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.os.Handler.dispatchMessage(Handler.java:99)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.os.Looper.loop(Looper.java:123)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at android.app.ActivityThread.main(ActivityThread.java:4627)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at java.lang.reflect.Method.invokeNative(Native Method)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at java.lang.reflect.Method.invoke(Method.java:521)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 10-16 15:15:44.011: E/AndroidRuntime(731):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

10-16 15:15:44.011:E / AndroidRuntime(731):at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:2)

将上下文传递给适配器,如下所示 -

 listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);

答案 1 :(得分:1)

DemoActivity定义上下文mContext=DemoActivity.this.getActivity();而不只是Context

您必须使用getActivity(),它返回与片段关联的活动 活动是一个上下文(因为活动扩展了上下文)。

<强>编辑:

在您的上下文中执行以下操作:

public DemoActivity(FragmentActivity context) {
    this.mContext=context;
}