AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 77586552
Accepted
Daniel Walker
Daniel Walker
Asked: 2023-12-01 23:59:25 +0800 CST2023-12-01 23:59:25 +0800 CST 2023-12-01 23:59:25 +0800 CST

单击文本字段后按钮无响应

  • 772

我刚刚开始 Android 应用程序开发,在实现OnClickListener. 我从 Android Studio 中的“空视图活动”开始。我在其他 UI 元素中添加了 a Button、 anEditText和ScrollView包含 a 的 a TextView。我OnClickListener在按钮上设置了一个,以便发出 logcat 消息。

当我单击该按钮时,它会按预期发出 logcat 消息。但是,一旦我单击文本字段,该按钮就会在应用程序的剩余生命周期内变得无响应。奇怪的是,如果我删除ScrollView.

这是我的 MRE:

活动主文件

<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/startErrorText"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/startButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="160dp"
        android:layout_marginTop="30dp"
        android:layout_marginEnd="160dp"
        android:text="Start"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textField" />

    <EditText
        android:id="@+id/textField"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="80dp"
        android:layout_marginTop="48dp"
        android:layout_marginEnd="80dp"
        android:ems="10"
        android:hint="Enter some text"
        android:inputType="text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="409dp"
        android:layout_height="585dp"
        android:layout_marginStart="1dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="1dp"
        android:layout_marginBottom="2dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/startButton"
        app:layout_constraintVertical_bias="1.0">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/logView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

package com.example.test

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.View.OnClickListener
import android.widget.Button

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val button: Button = findViewById(R.id.startButton)
        button.setOnClickListener(object : OnClickListener {
            override fun onClick(v: View?) {
                Log.i("MyApp", "Button pressed")
            }
        })
    }
}

这是 logcat 输出:

ziparchive              com.example.test                     W  Unable to open '/data/data/com.example.test/code_cache/.overlay/base.apk/classes3.dm': No such file or directory
ziparchive              com.example.test                     W  Unable to open '/data/app/~~6ohH-jaL6kb5-3S5sLz1rw==/com.example.test-21xtpoSa2I8lnFAMirvZRA==/base.dm': No such file or directory
ziparchive              com.example.test                     W  Unable to open '/data/app/~~6ohH-jaL6kb5-3S5sLz1rw==/com.example.test-21xtpoSa2I8lnFAMirvZRA==/base.dm': No such file or directory
nativeloader            com.example.test                     D  Configuring clns-6 for other apk /data/app/~~6ohH-jaL6kb5-3S5sLz1rw==/com.example.test-21xtpoSa2I8lnFAMirvZRA==/base.apk. target_sdk_version=34, uses_libraries=, library_path=/data/app/~~6ohH-jaL6kb5-3S5sLz1rw==/com.example.test-21xtpoSa2I8lnFAMirvZRA==/lib/arm64, permitted_path=/data:/mnt/expand:/data/user/0/com.example.test
GraphicsEnvironment     com.example.test                     V  Currently set values for:
GraphicsEnvironment     com.example.test                     V    angle_gl_driver_selection_pkgs=[]
GraphicsEnvironment     com.example.test                     V    angle_gl_driver_selection_values=[]
GraphicsEnvironment     com.example.test                     V  ANGLE GameManagerService for com.example.test: false
GraphicsEnvironment     com.example.test                     V  com.example.test is not listed in per-application setting
GraphicsEnvironment     com.example.test                     V  Neither updatable production driver nor prerelease driver is supported.
libEGL                  com.example.test                     D  loaded /vendor/lib64/egl/libEGL_emulation.so
AppCompatDelegate       com.example.test                     D  Checking for metadata for AppLocalesMetadataHolderService : Service not found
libEGL                  com.example.test                     D  loaded /vendor/lib64/egl/libGLESv1_CM_emulation.so
libEGL                  com.example.test                     D  loaded /vendor/lib64/egl/libGLESv2_emulation.so
om.example.test         com.example.test                     W  Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (unsupported, reflection, allowed)
om.example.test         com.example.test                     W  Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (unsupported, reflection, allowed)
Compatibil...geReporter com.example.test                     D  Compat change id reported: 210923482; UID 10191; state: ENABLED
Compatibil...geReporter com.example.test                     D  Compat change id reported: 171228096; UID 10191; state: ENABLED
Compatibil...geReporter com.example.test                     D  Compat change id reported: 237531167; UID 10191; state: DISABLED
OpenGLRenderer          com.example.test                     W  Unknown dataspace 0
OpenGLRenderer          com.example.test                     W  Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
OpenGLRenderer          com.example.test                     W  Failed to initialize 101010-2 format, error = EGL_SUCCESS
Gralloc4                com.example.test                     I  mapper 4.x is not supported
OpenGLRenderer          com.example.test                     E  Unable to match the desired swap behavior.
AutofillManager         com.example.test                     D  notifyViewEnteredForFillDialog:1073741824
EGL_emulation           com.example.test                     D  app_time_stats: avg=145.48ms min=0.87ms max=1433.47ms count=10
EGL_emulation           com.example.test                     D  app_time_stats: avg=181.58ms min=13.65ms max=2822.69ms count=17
MyApp                   com.example.test                     I  Button pressed
ProfileInstaller        com.example.test                     D  Installing profile for com.example.test
EGL_emulation           com.example.test                     D  app_time_stats: avg=39.13ms min=0.77ms max=1255.29ms count=48
Compatibil...geReporter com.example.test                     D  Compat change id reported: 163400105; UID 10191; state: ENABLED
ImeTracker              com.example.test                     I  com.example.test:39ecb7d6: onRequestShow at ORIGIN_CLIENT_SHOW_SOFT_INPUT reason SHOW_SOFT_INPUT
InputMethodManager      com.example.test                     D  showSoftInput() view=androidx.appcompat.widget.AppCompatEditText{4a6450d VFED..CL. .F.P..ID 250,132-830,256 #7f0801f9 app:id/textField aid=1073741824} flags=0 reason=SHOW_SOFT_INPUT
AssistStructure         com.example.test                     I  Flattened final assist data: 1492 bytes, containing 1 windows, 8 views
InsetsController        com.example.test                     D  show(ime(), fromIme=true)
InteractionJankMonitor  com.example.test                     D  Build configuration failed!
                                                                                                    java.lang.IllegalArgumentException: Must pass in a valid surface control if only instrument surface; 
                                                                                                        at com.android.internal.jank.InteractionJankMonitor$Configuration.validate(InteractionJankMonitor.java:1259)
                                                                                                        at com.android.internal.jank.InteractionJankMonitor$Configuration.<init>(InteractionJankMonitor.java:1217)
                                                                                                        at com.android.internal.jank.InteractionJankMonitor$Configuration.<init>(Unknown Source:0)
                                                                                                        at com.android.internal.jank.InteractionJankMonitor$Configuration$Builder.build(InteractionJankMonitor.java:1197)
                                                                                                        at com.android.internal.jank.InteractionJankMonitor.begin(InteractionJankMonitor.java:611)
                                                                                                        at android.view.inputmethod.ImeTracker$ImeJankTracker.onRequestAnimation(ImeTracker.java:717)
                                                                                                        at android.view.InsetsController$InternalAnimationControlListener$2.onAnimationStart(InsetsController.java:448)
                                                                                                        at android.animation.Animator$AnimatorListener.onAnimationStart(Animator.java:695)
                                                                                                        at android.animation.Animator$AnimatorCaller$$ExternalSyntheticLambda0.call(Unknown Source:4)
                                                                                                        at android.animation.Animator.callOnList(Animator.java:669)
                                                                                                        at android.animation.Animator.notifyListeners(Animator.java:608)
                                                                                                        at android.animation.Animator.notifyStartListeners(Animator.java:625)
                                                                                                        at android.animation.ValueAnimator.startAnimation(ValueAnimator.java:1334)
                                                                                                        at android.animation.ValueAnimator.start(ValueAnimator.java:1149)
                                                                                                        at android.animation.ValueAnimator.start(ValueAnimator.java:1173)
                                                                                                        at android.view.InsetsController$InternalAnimationControlListener.onReady(InsetsController.java:470)
                                                                                                        at android.view.InsetsAnimationThreadControlRunner.lambda$new$0(InsetsAnimationThreadControlRunner.java:129)
                                                                                                        at android.view.InsetsAnimationThreadControlRunner.$r8$lambda$3zGKYd3XPzPnvMO2hiF8a88M6T0(Unknown Source:0)
                                                                                                        at android.view.InsetsAnimationThreadControlRunner$$ExternalSyntheticLambda2.run(Unknown Source:6)
                                                                                                        at android.os.Handler.handleCallback(Handler.java:958)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                                        at android.os.Looper.loopOnce(Looper.java:205)
                                                                                                        at android.os.Looper.loop(Looper.java:294)
                                                                                                        at android.os.HandlerThread.run(HandlerThread.java:67)
ImeTracker              com.example.test                     I  com.example.test:39ecb7d6: onShown
EGL_emulation           com.example.test                     D  app_time_stats: avg=213.12ms min=0.85ms max=500.33ms count=5
EGL_emulation           com.example.test                     D  app_time_stats: avg=500.68ms min=499.36ms max=502.83ms count=3

这种情况java.lang.IllegalArgumentException只是偶尔发生,但总体错误每次都会发生。

  • 2 2 个回答
  • 40 Views

2 个回答

  • Voted
  1. Cloverleaf
    2023-12-02T01:29:53+08:002023-12-02T01:29:53+08:00

    我在我的 Android Studio 中运行了你的代码:ScrollView重叠了button一半。如果我activity_main.xml在该部分中替换<ScrollView该行

    app:layout_constraintVertical_bias="1.0">
    

    经过

    app:layout_constraintVertical_bias="0.0">
    

    一切正常。然而,一般来说,您可以activity_main.xml更简单、更清晰地进行编程。

    • 1
  2. Best Answer
    Nazarii Moshenskiy
    2023-12-02T01:48:17+08:002023-12-02T01:48:17+08:00

    当您单击 时EditText,键盘会弹出,并且 的空间较少ScrollView。因为你的ScrollView尺寸是固定的,所以它会与按钮重叠。Layout Inspector 您可以在我为您制作的屏幕截图中看到它:

    在此输入图像描述

    解决方案:您需要将ScrollView高度设置为0dp。ConstraintLayout将其解释为“填充所有可用空间”。当键盘弹出时,它只会占用可用空间,不会与按钮重叠。

    只要复制这个 xml 就可以了:

    <?xml version="1.0" encoding="utf-8"?>
    <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:id="@+id/startErrorText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <Button
            android:id="@+id/startButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="160dp"
            android:layout_marginTop="30dp"
            android:layout_marginEnd="160dp"
            android:text="Start"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textField" />
    
        <EditText
            android:id="@+id/textField"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="80dp"
            android:layout_marginTop="48dp"
            android:layout_marginEnd="80dp"
            android:ems="10"
            android:hint="Enter some text"
            android:inputType="text"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <ScrollView
            android:id="@+id/scrollView"
            android:layout_width="409dp"
            android:layout_height="0dp"
            android:layout_marginStart="1dp"
            android:layout_marginTop="20dp"
            android:layout_marginEnd="1dp"
            android:layout_marginBottom="2dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/startButton">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
    
                <TextView
                    android:id="@+id/logView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </LinearLayout>
        </ScrollView>
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    这是你的 ScrollView 现在的样子(检查它使用的空间): 在此输入图像描述

    建议:如果您使用,ConstraintLayout请尽量不要对大小进行硬编码,并尽可能依赖约束。

    • 1

相关问题

  • 将复制活动的序列号添加到 Blob

  • Packer 动态源重复工件

  • 选择每组连续 1 的行

  • 图形 API 调用列表 subscribedSkus 状态权限不足,但已授予权限

  • 根据列值创建单独的 DF 的函数

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    使用 <font color="#xxx"> 突出显示 html 中的代码

    • 2 个回答
  • Marko Smith

    为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类?

    • 1 个回答
  • Marko Smith

    您可以使用花括号初始化列表作为(默认)模板参数吗?

    • 2 个回答
  • Marko Smith

    为什么列表推导式在内部创建一个函数?

    • 1 个回答
  • Marko Smith

    我正在尝试仅使用海龟随机和数学模块来制作吃豆人游戏

    • 1 个回答
  • Marko Smith

    java.lang.NoSuchMethodError: 'void org.openqa.selenium.remote.http.ClientConfig.<init>(java.net.URI, java.time.Duration, java.time.Duratio

    • 3 个回答
  • Marko Smith

    为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)?

    • 4 个回答
  • Marko Smith

    为什么库中不调用全局变量的构造函数?

    • 1 个回答
  • Marko Smith

    std::common_reference_with 在元组上的行为不一致。哪个是对的?

    • 1 个回答
  • Marko Smith

    C++17 中 std::byte 只能按位运算?

    • 1 个回答
  • Martin Hope
    fbrereto 为什么在传递 {} 时重载解析更喜欢 std::nullptr_t 而不是类? 2023-12-21 00:31:04 +0800 CST
  • Martin Hope
    比尔盖子 您可以使用花括号初始化列表作为(默认)模板参数吗? 2023-12-17 10:02:06 +0800 CST
  • Martin Hope
    Amir reza Riahi 为什么列表推导式在内部创建一个函数? 2023-11-16 20:53:19 +0800 CST
  • Martin Hope
    Michael A fmt 格式 %H:%M:%S 不带小数 2023-11-11 01:13:05 +0800 CST
  • Martin Hope
    God I Hate Python C++20 的 std::views::filter 未正确过滤视图 2023-08-27 18:40:35 +0800 CST
  • Martin Hope
    LiDa Cute 为什么 'char -> int' 是提升,而 'char -> Short' 是转换(但不是提升)? 2023-08-24 20:46:59 +0800 CST
  • Martin Hope
    jabaa 为什么库中不调用全局变量的构造函数? 2023-08-18 07:15:20 +0800 CST
  • Martin Hope
    Panagiotis Syskakis std::common_reference_with 在元组上的行为不一致。哪个是对的? 2023-08-17 21:24:06 +0800 CST
  • Martin Hope
    Alex Guteniev 为什么编译器在这里错过矢量化? 2023-08-17 18:58:07 +0800 CST
  • Martin Hope
    wimalopaan C++17 中 std::byte 只能按位运算? 2023-08-17 17:13:58 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve