繁体   English   中英

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

[英]My app crashes when I try to implement a Scroll Bar

我的应用程序有问题,我最近开始在Android Studio中进行编码,但遇到了我无法解决的问题。 因此,我有一个包含4个活动的主页,当我在模拟器中运行程序时,该应用将打开,其中3个活动可以正常运行,但是当我单击以打开活动时,尝试实现滚动条的那个活动就会崩溃。

这是代码:

<?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:启动应用程序$ 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安装了拆分的APK $ adb shell开始-n“ com.example.android.coffeeknowledge / com.example.android.coffeeknowledge.MainActivity” -a android.intent.action.MAIN -c android。 intent.category.LAUNCHER客户端尚未准备就绪。等待进程上线已连接到设备模拟器5554上的进程10196,捕获并显示来自应用程序的logcat消息。 可以在“调试器”设置页面的“ Logcat输出”部分中禁用此行为。 D /:HostConnection :: get()建立新的主机连接0x8aa1c1c0,tid 10196 D /:HostConnection :: get()建立新的主机连接0x8aa1c540,tid 10218 I / OpenGLRenderer:已初始化的EGL版本1.4 D / OpenGLRenderer:交换行为1 W / OpenGLRenderer:无法选择具有EGL_SWAP_BEHAVIOR_PRESERVED的配置,无法重试而无需... D / OpenGLRenderer:交换行为0 D / EGL_emulation:eglCreateContext:0x8a9fe920:maj 2 min 0 rcv 2 D / EGL_emulation:eglMakeCurrent:0x8a9( 0x99d98910)W / art:在Android 4.1之前,方法int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int,boolean)方法会错误地覆盖android.widget.ListView中的package-private方法D / EGL_emulation:eglMakeCurrent:0x8a9fe920 :版本2 0(tinfo 0x99d98910)D / AndroidRuntime:关闭VM E / AndroidRuntime: 致命例外:主进程:com.example.android.coffeeknowledge,PID:10196 java.lang.RuntimeException:无法启动活动ComponentInfo {com。例。 android.coffeeknowledge / com.example.android.coffeeknowledge.coffeeGrowth}:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void android.view.View.setOnClickListener(android.view.View $ OnClickListener)”在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)在android.app.ActivityThread.-wrap12(ActivityThread.java)在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477)在android.os.Handler.dispatchMessage(Handler.java:102)在android.os.Looper.loop(Looper.java:154)在android.app.ActivityThread.main(位于com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)的java.lang.reflect.Method.invoke(本机方法)的ActivityThread.java:6119) ZygoteInit.main(ZygoteInit.java:776)原因:java.lang.NullPointerException:尝试调用虚拟方法'void android.view.View.set.OnClickL istener(android.view.View $ OnClickListener)在com.example.android.coffeeknowledge.coffeeGrowth.onCreate(coffeeGrowth.java:98)上的空对象引用上android.app.Activity.performCreate(Activity.java:6679)在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)在android.app。 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1477)上的ActivityThread.wrap12(ActivityThread.java)android.os.Looper.loop(上)的android.os.Handler.dispatchMessage(Handler.java:102) Looper.java:154),位于android.app.ActivityThread.main(ActivityThread.java:6119),位于java.lang.reflect.Method.invoke(本机方法),位于com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run( com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)处的ZygoteInit.java:886)应用程序终止。

谢谢你,达德

您的XML错误,我的朋友。

ScrollView不是布局选项。 它的看法。 因此,您需要将Layout包装在scrollview周围。 请记住,一个ScrollView只能有一个孩子。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM