我在NestedScrollView中包含了NestedScrollView,为了使NestedScrollView附加正确的高度和滚动属性,我将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
)。
我想知道是否有更好的方法来达到这种行为。
发布于 2018-02-12 15:40:58
好的,我使布局工作的方式是不要在xml中使用setNestedScrollingEnabled
方法,而是在运行时将它设置在您的活动或片段中,如下所示:
NestedScrollView view = (NestedScrollView) findViewById(R.id.nestedScroll);
view.setNestedScrollingEnabled(true);
这样做还可以减少所需的API级别,因为setNestedScrollingEnabled
中的API级别需要21级或更高级别。
https://stackoverflow.com/questions/48749726
复制相似问题