gpt4 book ai didi

java - 单击 Android ExpandableListView 会导致子项目出现 "ID Not Found"错误

转载 作者:行者123 更新时间:2023-11-30 03:55:08 33 4
gpt4 key购买 nike

我有一个 ExpandableListView,当我点击展开子项时,当我尝试更新 TextView 上的文本时,它会抛出“未找到”错误在子布局中。如果我将这部分注释掉,它就可以正常工作。我的项目基于我在周围找到的例子。谁能发现我哪里出错了?

这是子布局:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<TextView android:id="@+id/breakdown_item_a"
android:layout_height="40dip"
android:layout_width="40dip"
android:layout_marginLeft="40dip"
android:text="A" />

<TextView android:id="@+id/breakdown_item_b"
android:layout_height="40dip"
android:layout_width="40dip"
android:layout_marginLeft="40dip"
android:text="B" />
</LinearLayout>

然后是创建 subview 的代码:

public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View rowView, ViewGroup parent) {
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

rowView = inflater.inflate(R.layout.list_level_breakdown_item, null);
}
// If I comment out the next few lines it works, else it crashes here
((TextView)rowView.findViewById(R.id.breakdown_item_a)).setText(this.getChild(groupPosition, childPosition).getA()); ((TextView)rowView.findViewById(R.id.breakdown_item_b)).setText(this.getChild(groupPosition, childPosition).getB());

return rowView;
}

终于到了我的电脑,这是抛出的错误消息。查看堆栈跟踪,似乎是 View 出了问题,而不是 Id 本身的检索:

FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: String resource ID #0xa
at android.content.res.Resources.getText(Resources.java:247)
at android.widget.TextView.setText(TextView.java:3473)
at testing.android.arrayadapters.BreakdownArray.getChildView(BreakdownArray.java:50)
at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:450)
at android.widget.AbsListView.obtainView(AbsListView.java:2033)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
at android.widget.ListView.onMeasure(ListView.java:1155)
at android.view.View.measure(View.java:12723)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
at android.view.View.measure(View.java:12723)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12723)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12723)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1196)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:555)
at android.view.View.measure(View.java:12723)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12723)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
at android.view.View.measure(View.java:12723)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
at android.view.View.measure(View.java:12723)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

它似乎不喜欢测量某些东西。我更新了代码(布局和 subview )。我已经尝试将内容更新为固定宽度以查看是否有帮助。到目前为止没有运气将继续修补

最佳答案

难道仅仅是因为你有2个ID完全相同的 child ,所以当Android被要求通过ID获取元素时,它不知道要获取哪个?...

<LinearLayout>
<TextView android:id="@+id/breakdown_item" ...>
<TextView android:id="@+id/breakdown_item" ...>
</LinearLayout>

你可能应该改变 children 有不同的 ID,这样他们就独一无二,像这样......

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<TextView android:id="@+id/first_breakdown_item" android:layout_width="0px" android:layout_height="wrap_content" android:layout_weight="0.5" android:text="A" />
<TextView android:id="@+id/second_breakdown_item" android:layout_width="0px" android:layout_height="wrap_content" android:layout_weight="0.5" android:text="B" />
</LinearLayout>

关于java - 单击 Android ExpandableListView 会导致子项目出现 "ID Not Found"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13472891/

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