android:片段中的嵌套scrollview抛出异常

时间:2018-07-22 06:42:56

标签: android android-layout android-nestedscrollview

我有一种布局,如下所示

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/upperSroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:nestedScrollingEnabled="true"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ScrollView
                android:id="@+id/innerSroll"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">

                <LinearLayout
                    android:id="@+id/up"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="50dp"
                        android:text="csdsbfsfbsdf sfjdsfjdsfhjsd" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="50dp"
                        android:text="csdsbfsfbsdf sfjdsfjdsfhjsd" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="50dp"
                        android:text="csdsbfsfbsdf sfjdsfjdsfhjsd" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="50dp"
                        android:text="csdsbfsfbsdf sfjdsfjdsfhjsd" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="50dp"
                        android:text="csdsbfsfbsdf sfjdsfjdsfhjsd" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="50dp"
                        android:text="csdsbfsfbsdf sfjdsfjdsfhjsd" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="50dp"
                        android:text="csdsbfsfbsdf sfjdsfjdsfhjsd" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="50dp"
                        android:text="csdsbfsfbsdf sfjdsfjdsfhjsd" />
                </LinearLayout>
            </ScrollView>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@id/innerSroll"
                android:layout_weight="1"
                android:background="@color/colorAccent"
                android:orientation="vertical">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@drawable/ic_launcher_background" />
            </RelativeLayout>
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</RelativeLayout>

它在不添加android:nestedScrollingEnabled="true"的情况下运行良好。如果在布局中添加此行,则会在Fragment中出现以下异常

    07-22 12:07:33.694 13199-13199/com.example.mukesh.nestedscrollsample E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.mukesh.nestedscrollsample, PID: 13199
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mukesh.nestedscrollsample/com.example.mukesh.nestedscrollsample.MainActivity}: android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class android.support.v4.widget.NestedScrollView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3003)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3064)
        at android.app.ActivityThread.-wrap14(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1659)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6823)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
     Caused by: android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class android.support.v4.widget.NestedScrollView
     Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class android.support.v4.widget.NestedScrollView
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
        at android.view.LayoutInflater.createView(LayoutInflater.java:652)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:865)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:525)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
        at com.example.mukesh.nestedscrollsample.BlankFragment.onCreateView(BlankFragment.java:67)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:2425)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2623)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2410)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2365)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2272)
        at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3271)
        at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3227)
        at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:610)
        at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1256)
        at android.app.Activity.performStart(Activity.java:6994)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2956)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3064)
        at android.app.ActivityThread.-wrap14(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1659)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6823)
        at java.lang.reflect.Method.invoke(Native Method)
07-22 12:07:33.695 13199-13199/com.example.mukesh.nestedscrollsample E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1563)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1451)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.view.NestedScrollingChildHelper.setNestedScrollingEnabled(boolean)' on a null object reference
        at android.support.v4.widget.NestedScrollView.setNestedScrollingEnabled(NestedScrollView.java:251)
        at android.view.View.<init>(View.java:4877)
        at android.view.ViewGroup.<init>(ViewGroup.java:582)
        at android.widget.FrameLayout.<init>(FrameLayout.java:92)
        at android.widget.FrameLayout.<init>(FrameLayout.java:87)
        at android.support.v4.widget.NestedScrollView.<init>(NestedScrollView.java:198)
        at android.support.v4.widget.NestedScrollView.<init>(NestedScrollView.java:193)
            ... 36 more

我需要什么: 在普通模式下,内部滚动条应该起作用,而在多窗口模式下,内部滚动条应该露出所有项目,而外部滚动条应该起作用。

我尝试在代码中将NestedScrollEnable设置为false,但无法正常工作。

1 个答案:

答案 0 :(得分:0)

将嵌套的ScrollView替换为NestedScrollView

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ScrollView <-- This should be android.support.v4.widget.NestedScrollView -->
            android:id="@+id/innerSroll"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

并使用app作为命名空间:app:nestedScrollingEnabled="true"