gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:04:44 25 4
gpt4 key购买 nike

我的应用程序有问题,我最近开始在 Android Studio 中编码,但遇到了一个我无法解决的问题。
所以我有一个包含 4 个 Activity 的主页,当我在模拟器中运行程序时,应用程序打开并且其中 3 个 Activity 完美运行,但是当我单击打开 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.



谢谢,
杜德。

最佳答案

你的 XML 是错误的,我的 friend 。

ScrollView 不是布局选项。它的一个 View 。所以你需要将你的布局包裹在 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>

关于android - 当我尝试实现滚动条时,我的应用程序崩溃了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51157755/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com