user6490462
user6490462

Reputation:

NestedScrollView throw error "android.view.InflateException" when use nestedScrollEnabaled attribute

I have ViewPager inside NestedScrollView and in order to make my ViewPager attach the right height and scroll attributes I added android:nestedScrollingEnabled="true" to my NestedScroll.., here is a snap for my design :

    <?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent">

        <items .... />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/nestedScroll"
        android:nestedScrollingEnabled="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/_7sdp"
            android:layout_marginRight="@dimen/_7sdp"/>
    </android.support.v4.widget.NestedScrollView>


</android.support.design.widget.CoordinatorLayout>

The issue: Is when I set android:nestedScrollingEnabled="true" to true I got those exception:

java.lang.RuntimeException: Unable to start activity ComponentInfo{somePackege}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class <unknown>

AND

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.NestedScrollingChildHelper.setNestedScrollingEnabled(boolean)' on a null object reference

I'm confused if android:nestedScrollingEnabled="@otherAtt" take another attribute (Not boolean).

And I'm was wondering if there a better approach to reach this behavior.

Upvotes: 8

Views: 7817

Answers (1)

Anthony Cannon
Anthony Cannon

Reputation: 1273

Ok so the way I made your layout work is to not use the setNestedScrollingEnabled method in the xml and instead set it in runtime inside your activity or fragment like so:

NestedScrollView view = (NestedScrollView) findViewById(R.id.nestedScroll);
view.setNestedScrollingEnabled(true);

Doing it this way also reduces the API level needed, as to setNestedScrollingEnabled in xml requires API level 21 or higher.

Upvotes: 6

Related Questions