【问题标题】:NestedScrollView throw error "android.view.InflateException" when use nestedScrollEnabaled attributeNestedScrollView 在使用nestedScrollEnabaled 属性时抛出错误“android.view.InflateException”
【发布时间】:2018-07-22 19:33:30
【问题描述】:

我在 NestedScrollView 中有 ViewPager,为了让我的 ViewPager 附加正确的高度和滚动属性,我将 android:nestedScrollingEnabled="true" 添加到我的NestedScroll..,这是我的设计:

    <?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>

问题:当我将 android:nestedScrollingEnabled="true" 设置为 true 时,我得到了这些异常:

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>

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

如果android:nestedScrollingEnabled="@otherAtt" 采用另一个属性(不是boolean),我会感到困惑。

我想知道是否有更好的方法来实现这种行为。

【问题讨论】:

    标签: android android-viewpager android-coordinatorlayout android-nestedscrollview


    【解决方案1】:

    好的,所以我让你的布局工作的方式是不在 xml 中使用 setNestedScrollingEnabled 方法,而是在运行时在你的活动或片段中设置它,如下所示:

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

    这样做也降低了所需的 API 级别,因为 xml 中的 setNestedScrollingEnabled 需要 API 级别 21 或更高。

    【讨论】:

    • ``=setNestedScrollingEnabled in xml 需要 API 级别 21 或更高级别` true> 并使用 java 设置 setNestedScrollingEnabled(true); 不会导致应用程序崩溃,但这并不是最重要的事情。 t 为 viewPager 设置正确的高度,viewPager 被压缩并且没有附加到视图。
    • 三件事让我的布局崩溃了,两次你使用app:layout_behavior 和你使用android:nestedScrollingEnabled 的时间,所以我删除了layout_behavior 并移动了nestedScrollingEnabled
    • 为什么要用NestedScrollView,有什么具体原因吗?因为 ViewPager 能够处理滚动以在其片段之间进行转换。
    • 你看到CoordinatorLayout了吗?我用它来使用app:layout_behavior,这是一种滚动行为,viewPager 也包含 recycleView,它是另一种滚动。所以需要使用nestedscrollview。
    • 好的,那么如果我们删除此布局中的NestedScrollView 并为每个RecycleView's 调用setNestedScrollingEnabled 怎么样?
    猜你喜欢
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 2017-08-04
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 2017-09-01
    • 1970-01-01
    相关资源
    最近更新 更多