DavidJ
DavidJ

Reputation: 27

My app crashes when I try to implement a Scroll Bar

I have a problem with my app, i started coding in Android studio recently and i got a problem i can`t solve. So i have a main page with 4 activities, when i run the program in the emulator the app opens and 3 of the activities work perfectly but the one where i try to implement the scrollbar crashes when i click to open the activity.

Here's the code:

<?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>

And the crash:

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.

Thank you, Daud.

Upvotes: 0

Views: 697

Answers (1)

letsCode
letsCode

Reputation: 3044

Your XML is wrong my friend.

A ScrollView is not a layout option. Its a view. So you need to wrap your Layout around the scrollview. Keep in mind, a ScrollView can only have ONE 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>

Upvotes: 0

Related Questions