如何解决AS ..中的问题? ScrollView.setOnScrollChangeListener

时间:2019-11-11 11:20:36

标签: android scrollview

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/scrollMenu"
    android:layout_height="wrap_content">



 @RequiresApi(api = Build.VERSION_CODES.P)
    private void initViews() {
        scrollMenu = findViewById(R.id.scrollMenu);
        scrollMenu.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

            }
        });
//        }

E / Android运行时:致命异常:主要     流程:com.dsmsoft.traccia.sampleui,PID:12790     java.lang.RuntimeException:无法启动活动ComponentInfo {com.dsmsoft.traccia.sampleui / com.dsmsoft.traccia.sampleui.MainActivity}:java.lang.NullPointerException:尝试调用虚拟方法'void android.widget.ScrollView。在空对象引用上的setOnScrollChangeListener(android.view.View $ OnScrollChangeListener)'         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)         在android.app.ActivityThread.-wrap11(未知来源:0)         在ndroid.app.ActivityThread $ H.handleMessage(ActivityThread.java:1589)         在android.os.Handler.dispatchMessage(Handler.java:106)         在android.os.Looper.loop(Looper.java:164)         在android.app.ActivityThread.main(ActivityThread.java:6494)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)      原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.ScrollView.setOnScrollChangeListener(android.view.View $ OnScrollChangeListener)'         在com.dsmsoft.traccia.sampleui.MainActivity.initViews(MainActivity.java:25)         在com.dsmsoft.traccia.sampleui.MainActivity.onCreate(MainActivity.java:19)         在android.app.Activity.performCreate(Activity.java:7009)         在android.app.Activity.performCreate(Activity.java:7000)         在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)         在android.app.ActivityThread.-wrap11(未知来源:0)         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1589)         在android.os.Handler.dispatchMessage(Handler.java:106)         在android.os.Looper.loop(Looper.java:164)         在android.app.ActivityThread.main(ActivityThread.java:6494)         在java.lang.reflect.Method.invoke(本机方法)         在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438)         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 进程12790终止。

2 个答案:

答案 0 :(得分:0)

您的窗口小部件setOnScrollListener为null。您忘记了findViewById()吗?

答案 1 :(得分:0)

您可以在滚动视图中添加其他任何布局,例如约束布局和内部布局

<androidx.constraintlayout.widget.ConstraintLayout
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">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">


        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="104dp"
            android:layout_height="109dp"
            android:layout_marginTop="32dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@android:drawable/screen_background_light" />

        <TextView
            android:id="@+id/firstname"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="32dp"
            android:text="First Name :"
            android:textColor="@android:color/black"
            app:layout_constraintEnd_toStartOf="@+id/textView14"
            app:layout_constraintHorizontal_bias="0.217"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView4" />
 </androidx.constraintlayout.widget.ConstraintLayout>
    </ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>