【问题标题】:Android: can not find parent view [duplicate]Android:找不到父视图[重复]
【发布时间】:2017-02-24 12:46:20
【问题描述】:

我的文件layout.xml

<android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/edit_name"
            android:hint="@string/name"
            />
</android.support.design.widget.TextInputLayout>

当我尝试让父母参与活动时

 EditText edit_name= (EditText) view.findViewById(R.id.edit_name);
 TextInputLayout textInputLayout = (TextInputLayout) edit_name.getParent();

我得到错误

java.lang.ClassCastException: android.widget.FrameLayout 无法转换为 android.support.design.widget.TextInputLayout

【问题讨论】:

  • 下面我的回答你看过了吗,对你有用吗?

标签: android view get parent


【解决方案1】:

您需要在 TextInputLayout 中设置 id 并使用 id 绑定而不是使用 getParent()。

谢谢。

【讨论】:

  • 感谢您的帮助
【解决方案2】:

一种解决方法是为此 TextInputLayout 设置一个 id:

<android.support.design.widget.TextInputLayout
        android:id="@+id/edit_input_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/edit_name"
            android:hint="@string/name"
            />
</android.support.design.widget.TextInputLayout>

EditText edit_name= (EditText) view.findViewById(R.id.edit_name);
 TextInputLayout textInputLayout = (TextInputLayout) view.findViewById(R.id.edit_input_layout);

【讨论】:

    【解决方案3】:

    首先,您必须像这样向 TextInputLayout 添加一组 Id:

    <android.support.design.widget.TextInputLayout
            android:id="@+id/text_input_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            >
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/edit_name"
                android:hint="@string/name"
                />
    </android.support.design.widget.TextInputLayout>
    

    之后,您可以像获得 EditText 一样获得 TextInputLayout。

    【讨论】:

      【解决方案4】:

      也许您想以不同的方式尝试TextInputLayout

      而不是使用:

      TextInputLayout textInputLayout = (TextInputLayout) edit_name.getParent();
      

      你可能想试试:

      XML:

      <android.support.design.widget.TextInputLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:id="testId"        >
              <EditText
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:id="@+id/edit_name"
                  android:hint="@string/name"
                  />
      </android.support.design.widget.TextInputLayout>
      

      代码:

      TextInputLayout textInputLayout = (TextInputLayout) findViewById(R.id.testId);
      

      【讨论】:

        【解决方案5】:

        注意:TextInputLayout 下的实际视图层次结构不是 保证与用 XML 编写的视图层次结构相匹配。因此, 在 TextInputLayout 的子级上调用 getParent() - 例如 TextInputEditText -- 可能不会返回 TextInputLayout 本身,但 而是一个中间视图。如果您需要直接访问视图, 设置一个 android:id 并使用 findViewById(int)。

        即要解决此问题,您必须使用 findViewById 而不是 getParent

        来源是Android official documents:

        【讨论】:

          猜你喜欢
          • 2018-12-14
          • 2018-06-30
          • 1970-01-01
          • 2013-10-27
          • 1970-01-01
          • 2011-11-22
          • 2015-02-24
          • 2016-06-09
          • 1970-01-01
          相关资源
          最近更新 更多