我在NestedScrollView中有ViewPager,为了使ViewPager附加正确的高度和滚动属性,我在NestedScroll中添加了android:nestedScrollingEnabled="true"
。
<?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或更高。