简体   繁体   English

单击Android ExpandableListView会导致子项出现“找不到ID”错误

[英]Clicking on Android ExpandableListView causes “ID Not Found” error on child item

I've got a ExpandableListView which when I click to expand the child it throws a "Id not found" error when I try and update the Text on a TextView in the child Layout. 我有一个ExpandableListView ,当我单击以扩展子级时,当尝试在子版式中更新TextView上的文本时,它将引发“找不到ID”错误。 If I comment this section out it works fine. 如果我对此部分进行评论,则效果很好。 I've based my project off examples I've found round the place. 我的项目基于我在周围发现的示例。 Can anyone spot where I've gone wrong? 谁能发现我哪里出问题了?

Here's the child layout: 这是子布局:

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

And then here is the code that creates the child view: 然后是创建子视图的代码:

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;
}

Finally got to my PC and this is the error message that is being thrown. 终于到了我的电脑,这是正在抛出的错误消息。 Looking at the stack trace it seems to be something wrong with the view not the retrieval of the Id itself: 查看堆栈跟踪信息,似乎是错误的,而不是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)

Seems that it doesn't like the measurement of something. 似乎它不喜欢测量某些东西。 I've updated the code (layout and child view). 我已经更新了代码(布局和子视图)。 I've tried updating things to a fixed width to see if that helps. 我尝试将事物更新为固定宽度,以查看是否有帮助。 No luck so far will continue tinkering 到目前为止没有运气会继续修补

Is it simply that you have 2 children with exactly the same ID, so when Android is asked to get the element by ID, it doesn't know which one to get?... 仅仅是因为您有2个孩子具有完全相同的ID,所以当要求Android通过ID获取元素时,它不知道要获取哪个?

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

You should probably change the children to have different IDs, so that they're unique, something like this... 您可能应该将子代更改为具有不同的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>

Ok I've found out the reason. 好的,我找到了原因。 It would seem that the getA() method since it returned an int this caused it to crash as it doesn't auto convert it to a String. 似乎getA()方法由于返回了一个int,导致它崩溃了,因为它没有自动将其转换为String。 So that was nice pain in the ass for no reason at all :( sorry bout the confusion guys. But the overall lesson is: 因此,这是无缘无故的屁股痛:(对不起那些困惑的家伙。但是总的教训是:

If you're going to assign the text value of a textview ALWAYS explictly convert to a string when passing in the parameter as it wont throw errors...but will crash when you run it. 如果要分配textview的文本值,则在传递参数时总是将其明确转换为字符串,因为它不会引发错误...但是在运行时会崩溃。

Cheers for looking at it though. 虽然为它加油打气。

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

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