2

ListView inside ScrollView works perfectly on android API greater than 18 but, with lower than API 18 app crash showing java.lang.NullPointerException in line listItem.measure(0, 0); on MyUtils.java

MyUtils.java

    public static void setListViewHeightBasedOnChildren(ExpandableListView listView) {

        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;

        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();

    }

fragment_list.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="sportsnewsforcrazysportpepllatestnewsjstwaao.com.sportsnews.fragments.Frag_List">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <com.android.volley.toolbox.NetworkImageView
            android:id="@+id/networkImageView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:background="@color/divider"
            android:minHeight="150dp"/>

        <ExpandableListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/expandableListView"
            android:scrollbars="none"
            android:background="@color/app_background"
            android:childDivider="@color/app_background"/>

    </LinearLayout>
</ScrollView>

Fragment_list.java

    adapter = new ExpandableListViewAdapter(getActivity() , dataList);
    expandableListView.setAdapter(adapter);
    MyUtils.setListViewHeightBasedOnChildren(expandableListView);
    expandableListView.setOnGroupClickListener(this);
    expandableListView.setOnChildClickListener(this);
jazzbpn
  • 6,441
  • 16
  • 63
  • 99
  • Because nested scrollable `Views` were not supported until later API levels. It's best to still avoid nesting scrollable `Views`. No matter what your layout looks like there is always a way around it. – Xaver Kapeller Nov 27 '15 at 07:58
  • Thanks for your answer @Xaver . I know it's good idea to avoid nesting scrollable Views But, in some cases we need it cause of that I am asking. And another thing, Please don't mind but the above answer is not the correct solution of the asked query. – jazzbpn Nov 27 '15 at 09:53
  • Well the point I tried to make in my previous comment is that it is never really needed. There are workarounds for lower APIs which kind of make nested scrolling work, but they all come with their own set of drawbacks. If you want to implement this properly then don't nest two scrollable views. You can just use one scrollable `View` to achieve the same thing. And if I really had to nest two scrollable `Views` for whatever reason then why not use a `RecyclerView` or `NestedScrollViews` from the support library which back port the nested scrolling behavior to older apis? – Xaver Kapeller Nov 27 '15 at 09:59
  • Oo Yeah, RecyclerView is the best idea.. Thank @Xaver – jazzbpn Nov 27 '15 at 10:14

1 Answers1

0

In my app,if listview item layout's root view is RelativeLayout,it will cause this crash.Because in api 18 RelativeLayout source code

if(mLayoutParams.width >= 0) { width = Math.max(width, mLayoutParams.width); }

and in api >=19 RelativeLayout source code

if(mLayoutParams != null && mLayoutParams.width >= 0) { width = Math.max(width, mLayoutParams.width); }

the mLayoutParams maybe null if you inflate item view in this method:

contentView = (ViewGroup) LayoutInflater. rom(context).inflate(R.layout.select_text_oprate_window, null);

So,you can use this method:

contentView = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.select_text_oprate_window, parent,false);

Also you can change item layout's root view to LinearLayout.

turbofan
  • 318
  • 3
  • 7