首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用NestedScrollView属性时抛出错误"android.view.InflateException“

使用NestedScrollView属性时抛出错误"android.view.InflateException“
EN

Stack Overflow用户
提问于 2018-02-12 15:21:38
回答 1查看 6.6K关注 0票数 7

我在NestedScrollView中包含了NestedScrollView,为了使NestedScrollView附加正确的高度和滚动属性,我将android:nestedScrollingEnabled="true"添加到了NestedScroll中。下面是我的设计的一个快照:

代码语言:javascript
运行
复制
    <?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时,我得到了以下异常:

代码语言:javascript
运行
复制
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>

代码语言:javascript
运行
复制
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)。

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

EN

回答 1

Stack Overflow用户

发布于 2018-02-12 15:40:58

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

代码语言:javascript
运行
复制
NestedScrollView view = (NestedScrollView) findViewById(R.id.nestedScroll);
view.setNestedScrollingEnabled(true);

这样做还可以减少所需的API级别,因为setNestedScrollingEnabled中的API级别需要21级或更高级别。

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48749726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档