【问题标题】:ExpandableListView duplicate id problem - how to fix that?ExpandableListView 重复 id 问题 - 如何解决?
【发布时间】:2018-10-06 10:36:58
【问题描述】:

我正在尝试按照这个很棒的教程(https://www.youtube.com/watch?v=c4XagtQlqS0)制作Expandable ListView。我遇到的问题是当我想“添加”第二个可扩展文本时。我得到的错误是“重复的 id @+id/expandable_text 最初定义在这里”和“重复的 id @+id/expand_collapse,已经在此布局的前面定义”。这是一个逻辑错误,但在教程中没有显示所以我的代码有问题吗?

你可以在下面找到我的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="16dp"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:paddingTop="16dp"
    tools:context=".Body1">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.ms.square.android.expandabletextview.ExpandableTextView
        android:id="@+id/expandable_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:maxCollapsedLines="1"
        app:animDuration="200"
        >

        <TextView
            android:id="@+id/expandable_text"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="16dp"
            android:textColor="#666"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ImageButton
            android:id="@+id/expand_collapse"
            android:layout_gravity="right|bottom"
            android:background="@android:color/transparent"
            android:padding="16dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </com.ms.square.android.expandabletextview.ExpandableTextView>

    <com.ms.square.android.expandabletextview.ExpandableTextView
        android:id="@+id/expandable_text_view2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:maxCollapsedLines="1"
        app:animDuration="200"
        >

        <TextView
            android:id="@+id/expandable_text"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="16dp"
            android:textColor="#666"
            android:textSize="16sp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <ImageButton
            android:id="@+id/expand_collapse"
            android:layout_gravity="right|bottom"
            android:background="@android:color/transparent"
            android:padding="16dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </com.ms.square.android.expandabletextview.ExpandableTextView>
</ScrollView>


</LinearLayout>

还有java文件:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.ms.square.android.expandabletextview.ExpandableTextView;

public class Body1 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_body1);
        ExpandableTextView  expandableTextView1 = (ExpandableTextView) findViewById(R.id.expandable_text_view);
        expandableTextView1.setText(getString(R.string.text1));

        ExpandableTextView  expandableTextView2 = (ExpandableTextView) findViewById(R.id.expandable_text_view);
        expandableTextView2.setText(getString(R.string.text2));

    }
}

提前感谢您的帮助!

【问题讨论】:

    标签: android expandablelistview


    【解决方案1】:

    R.id.expandable_text_view2 用于第二个可展开的文本视图。

    ExpandableTextView  expandableTextView1 = (ExpandableTextView) findViewById(R.id.expandable_text_view);
            expandableTextView1.setText(getString(R.string.text1));
    
            ExpandableTextView  expandableTextView2 = (ExpandableTextView) findViewById(R.id.expandable_text_view2);
            expandableTextView2.setText(getString(R.string.text2));
    

    【讨论】:

      【解决方案2】:

      您为 textview 和 imagebutton 使用相同的 ID,您需要在定义布局视图时使用唯一 ID。

      如下修改你的代码

      <ScrollView
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
      
          <com.ms.square.android.expandabletextview.ExpandableTextView
              android:id="@+id/expandable_text_view"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:maxCollapsedLines="1"
              app:animDuration="200"
              >
      
              <TextView
                  android:id="@id/expandable_text"
                  android:layout_marginLeft="10dp"
                  android:layout_marginRight="10dp"
                  android:layout_marginTop="16dp"
                  android:textColor="#666"
                  android:textSize="16sp"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content" />
      
              <ImageButton
                  android:id="@id/expand_collapse"
                  android:layout_gravity="right|bottom"
                  android:background="@android:color/transparent"
                  android:padding="16dp"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content" />
          </com.ms.square.android.expandabletextview.ExpandableTextView>
      
          <com.ms.square.android.expandabletextview.ExpandableTextView
              android:id="@+id/expandable_text_view2"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:maxCollapsedLines="1"
              app:animDuration="200"
              >
      
              <TextView
                  android:id="@id/expandable_text"
                  android:layout_marginLeft="10dp"
                  android:layout_marginRight="10dp"
                  android:layout_marginTop="16dp"
                  android:textColor="#666"
                  android:textSize="16sp"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content" />
      
              <ImageButton
                  android:id="@id/expand_collapse"
                  android:layout_gravity="right|bottom"
                  android:background="@android:color/transparent"
                  android:padding="16dp"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content" />
          </com.ms.square.android.expandabletextview.ExpandableTextView>
      </ScrollView>
      

      【讨论】:

      • 首先感谢您的回答。我正在使用 ExpandableTextView 库,这就是我没有 ExpandableListView 的原因。同样根据这个答案:stackoverflow.com/questions/46032610/… 和教程中的“@id/expandable_text”和“@id/expand_collapse”必须相同
      • @EdwardCooper 更新的答案请检查。在第一部分的代码中,您使用 @+id/ 创建 id,您需要使用 @id
      【解决方案3】:

      在您的布局中有 2 个具有相同 ID 的 ImageButton。首先,更改第二个ID:

      <ImageButton
              android:id="@+id/expand_collapse_two"
              android:layout_gravity="right|bottom"
              android:background="@android:color/transparent"
              android:padding="16dp"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content" />
      

      在您的 Java 代码中,您声明了相同的 ExpandableTextView:

      ExpandableTextView  expandableTextView1 = (ExpandableTextView) findViewById(R.id.expandable_text_view);
      ExpandableTextView  expandableTextView2 = (ExpandableTextView) findViewById(R.id.expandable_text_view);
      

      所以你需要将expandableTextView2改为:

       ExpandableTextView  expandableTextView2 = (ExpandableTextView) findViewById(R.id.expandable_text_view2);
      

      最后,你的布局应该是这样的:

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout 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:layout_width="match_parent"
          android:layout_height="match_parent"
          android:paddingBottom="16dp"
          android:paddingLeft="16dp"
          android:paddingRight="16dp"
          android:paddingTop="16dp"
          tools:context=".Body1">
      
      <ScrollView
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
      
          <com.ms.square.android.expandabletextview.ExpandableTextView
              android:id="@+id/expandable_text_view"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:maxCollapsedLines="1"
              app:animDuration="200"
              >
      
              <TextView
                  android:id="@+id/expandable_text"
                  android:layout_marginLeft="10dp"
                  android:layout_marginRight="10dp"
                  android:layout_marginTop="16dp"
                  android:textColor="#666"
                  android:textSize="16sp"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content" />
      
              <ImageButton
                  android:id="@+id/expand_collapse"
                  android:layout_gravity="right|bottom"
                  android:background="@android:color/transparent"
                  android:padding="16dp"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content" />
          </com.ms.square.android.expandabletextview.ExpandableTextView>
      
          <com.ms.square.android.expandabletextview.ExpandableTextView
              android:id="@+id/expandable_text_view2"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:maxCollapsedLines="1"
              app:animDuration="200"
              >
      
              <TextView
                  android:id="@+id/expandable_text2"
                  android:layout_marginLeft="10dp"
                  android:layout_marginRight="10dp"
                  android:layout_marginTop="16dp"
                  android:textColor="#666"
                  android:textSize="16sp"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content" />
      
              <ImageButton
                  android:id="@+id/expand_collapse_two"
                  android:layout_gravity="right|bottom"
                  android:background="@android:color/transparent"
                  android:padding="16dp"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content" />
          </com.ms.square.android.expandabletextview.ExpandableTextView>
      </ScrollView>
      
      
      </LinearLayout>
      

      还有你的 Java 代码:

      import android.support.v7.app.AppCompatActivity;
      import android.os.Bundle;
      
      import com.ms.square.android.expandabletextview.ExpandableTextView;
      
      public class Body1 extends AppCompatActivity {
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_body1);
              ExpandableTextView  expandableTextView1 = (ExpandableTextView) findViewById(R.id.expandable_text_view);
              expandableTextView1.setText(getString(R.string.text1));
      
              ExpandableTextView  expandableTextView2 = (ExpandableTextView) findViewById(R.id.expandable_text_view2);
              expandableTextView2.setText(getString(R.string.text2));
      
          }
      }
      

      【讨论】:

      • 首先感谢您的回答!根据视频教程和这个答案:stackoverflow.com/questions/46032610/… '@id/expandable_text' 和 '@id/expand_collapse' 必须相同才能拥有多个可展开文本。
      • 刚刚注意到第二个 expandable_text ID 与第一个相同。查看新布局。现在,根据 XML,不能有 2 个或更多具有相同 ID 的元素。如果您使用不同的ID根本不起作用?如果没有,请尝试使用相同的 ID 并仅更改 Java 代码
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-11
      • 2010-09-15
      • 2019-06-07
      • 2017-04-13
      • 2021-12-09
      相关资源
      最近更新 更多