gpt4 book ai didi

Android:更改 View 顺序会导致 "Unfortunately, app has stopped"OK。为什么?

转载 作者:行者123 更新时间:2023-11-29 21:40:05 24 4
gpt4 key购买 nike

我有一个名为“Yamba”的应用程序。它拥有的唯一布局 XML 文件是:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical" >

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/titleStatus"
android:textSize="30sp" />

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top|center_horizontal"
android:hint="@string/hintText" />

<TextView
android:id="@+id/textCounter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp"
android:text="000" />

<Button
android:id="@+id/buttonId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/buttonUpdate"
android:textSize="20sp" />

</LinearLayout>

应用程序很好地展示了一切。

我所做的唯一更改是将 (EditText id/editText") 的顺序更改为 (TextView id/textCounter),使其变为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical" >

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/titleStatus"
android:textSize="30sp" />

<TextView
android:id="@+id/textCounter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="10dp"
android:text="000" />

<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top|center_horizontal"
android:hint="@string/hintText" />

<Button
android:id="@+id/buttonId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/buttonUpdate"
android:textSize="20sp" />

</LinearLayout>

此更改后,模拟器向我显示:不幸的是,Yamba 已停止"OK
当我将它恢复到之前的顺序时,一切正常。为什么?

最佳答案

当您更改 XML 布局中的 View 顺序时,它们的数字 ID 也会更改(它们从上到下分配),然后当您在代码中引用之前的 ID 时,例如在 findViewById() 中 你会得到错误的一个和 ClassCastException(我猜是因为你没有包含 logcat)。

build 前清洁将解决问题。

关于Android:更改 View 顺序会导致 "Unfortunately, app has stopped"OK。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17415331/

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