Android:GridView中的ExpandableListView

时间:2017-07-12 14:57:28

标签: android gridview expandablelistview expandablelistadapter

我正在解析json数据并尝试将它们放在GridView内的ExpandableListView中。解析完成没有任何错误,但我怀疑adepters存在问题。 我收到以下错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{myapp.app/myapp.app.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ExpandableListView.setOnGroupClickListener(android.widget.ExpandableListView$OnGroupClickListener)' on a null object reference

错误出现在下面显示的代码的第一行:

expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

        @Override
        public boolean onGroupClick(ExpandableListView parent, View v,
                                    int groupPosition, long id) {
            // Toast.makeText(getApplicationContext(),
            // "Group Clicked " + listDataHeader.get(groupPosition),
            // Toast.LENGTH_SHORT).show();
            return false;
        }
    });

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

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

    // ListView Group collapsed listener
    expListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {

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

        }
    });

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

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {
            Toast.makeText(
                    getApplicationContext(),
                    listDataHeader.get(groupPosition)
                            + " : "
                            + listDataChild.get(
                            listDataHeader.get(groupPosition)).get(
                            childPosition), Toast.LENGTH_SHORT)
                    .show();
            return false;
        }
    });

以下是grid_item_layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:background="@drawable/grid_color_selector"
android:orientation="vertical"
android:padding="0dp">

<TextView
    android:id="@+id/category_title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="5dp"
    android:ellipsize="marquee"
    android:maxLines="2"
    android:textSize="18sp" />

<com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/category_image"
    android:scaleType="fitCenter"
    android:layout_gravity="center"
    android:layout_width="140dp"
    android:layout_height="140dp"
    android:background="@color/primary" />

<ExpandableListView
    android:id="@+id/SubList"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</ExpandableListView>

0 个答案:

没有答案