ExpandableListView null对象参考

时间:2016-08-26 14:19:03

标签: android android-fragments nullpointerexception expandablelistview

我的ExpandableListView中有一个显示一些数据的Fragment。当我按下图标时,我会调用Fragment

public void searchClick(){
        Fragment frag = new Results();

        FragmentTransaction ft  = getSupportFragmentManager().beginTransaction();

        ft.replace(R.id.FragFaecher,frag).commit();



        displayList();


    }

displayList()然后应该填充List并显示它

public void displayList(){
        populateList();

        expList = (ExpandableListView) findViewById(R.id.expList);
        listAdapter = new ExpListAdapter(MainActivity.this,parentList);
        expList.setAdapter(listAdapter);

    }

在FragmentClass中,ExpandableListView的布局在fragment_results布局中膨胀

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
       View rootView = inflater.inflate(R.layout.fragment_results, container, false);


        return rootView;
    } 

片段布局:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="match_parent"
        android:id="@+id/results"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:background="?android:colorBackground"
        android:clickable="true">


       <ExpandableListView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="?android:colorBackground"
            android:childDivider="#000000"
            android:id="@+id/expList" />


    </RelativeLayout>

我想问题是,displayList()方法试图找到尚未创建的View。我在创建View时尝试返回一个布尔值,但这不起作用。有没有其他可能的方法可以防止这种情况发生?

0 个答案:

没有答案