gpt4 book ai didi

android - NestedScrollView 在使用 nestedScrollEnabaled 属性时抛出错误 "android.view.InflateException"

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:11:39 25 4
gpt4 key购买 nike

我在 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),我会感到困惑。

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

最佳答案

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

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

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

关于android - NestedScrollView 在使用 nestedScrollEnabaled 属性时抛出错误 "android.view.InflateException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48749726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com