当我尝试实现滚动条时,我的应用程序崩溃了

My app crashes when I try to implement a Scroll Bar

我的应用程序有问题,我最近开始在 Android 工作室编码,但我遇到了无法解决的问题。 所以我有一个包含 4 个活动的主页,当我 运行 模拟器中的程序打开应用程序时,其中 3 个活动完美运行,但是当我单击打开 activity。

代码如下:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingBottom="5dp"
    android:paddingTop="5dp" 
    tools:context=".coffeeGrowth" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

                <TextView
                    android:id="@+id/textView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:keepScreenOn="true"
                    android:text="@string/large_text"
                    android:textColor="#008000"
                    android:textSize="30sp"
                    android:textStyle="italic" />

        </RelativeLayout>


</ScrollView>

崩溃:

07/03 16:54:21: Launching app $ adb install-multiple -r -t -p com.example.android.coffeeknowledge C:\Users\Daud Jawad\CoffeeKnowledge\app\build\intermediates\instant-run-apk\debug\app-debug.apk Split APKs installed $ adb shell am start -n "com.example.android.coffeeknowledge/com.example.android.coffeeknowledge.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER Client not ready yet..Waiting for process to come online Connected to process 10196 on device emulator-5554 Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page. D/: HostConnection::get() New Host Connection established 0x8aa1c1c0, tid 10196 D/: HostConnection::get() New Host Connection established 0x8aa1c540, tid 10218 I/OpenGLRenderer: Initialized EGL, version 1.4 D/OpenGLRenderer: Swap behavior 1 W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without... D/OpenGLRenderer: Swap behavior 0 D/EGL_emulation: eglCreateContext: 0x8a9fe920: maj 2 min 0 rcv 2 D/EGL_emulation: eglMakeCurrent: 0x8a9fe920: ver 2 0 (tinfo 0x99d98910) W/art: Before Android 4.1, method int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView D/EGL_emulation: eglMakeCurrent: 0x8a9fe920: ver 2 0 (tinfo 0x99d98910) D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.coffeeknowledge, PID: 10196 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.coffeeknowledge/com.example.android.coffeeknowledge.coffeeGrowth}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) at android.app.ActivityThread.-wrap12(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.example.android.coffeeknowledge.coffeeGrowth.onCreate(coffeeGrowth.java:98) at android.app.Activity.performCreate(Activity.java:6679) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)  at android.app.ActivityThread.-wrap12(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6119)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)  Application terminated.

谢谢, Daud.

你的XML错了我的朋友。

ScrollView 不是布局选项。这是一个观点。因此,您需要将布局包裹在滚动视图周围。请记住,一个 ScrollView 只能有一个 child.

<?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"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingBottom="5dp"
    android:paddingTop="5dp" tools:context=".coffeeGrowth" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:keepScreenOn="true"
        android:text="@string/large_text"
        android:textColor="#008000"
        android:textSize="30sp"
        android:textStyle="italic" />

   </ScrollView>

</RelativeLayout>