2017-02-24 8 views
-1

マイファイル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をandroiにキャストできませんd.support.design.widget.TextInputLayout

+0

点検した、それはあなたに役立つでしょうか? –

答えて

0

TextInputLayoutにidを設定し、getParent()ではなくidでバインドする必要があります。

ありがとうございました。

+0

ご協力いただきありがとうございます –

0

回避策は、この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); 
0

まずあなたは、このようなあなたの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を取得できます。

0

おそらく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); 
0

注:TextInputLayoutの下に表示される実際のビュー階層は、XMLで記述されたビュー階層と一致することが保証されていません( )。結果として、 TextInputEditTextなどのTextInputLayoutの子でgetParent()を呼び出すと、 はTextInputLayout自体を返しませんが、中間のビューではなく を返すことがあります。ビューに直接アクセスする必要がある場合は、 にandroid:idを設定し、findViewById(int)を使用します。

つまり、問題を解決するには、getParentの代わりにfindViewByIdを使用する必要があります。

ソースAndroid official documentsされています。あなたは以下の私の答えを

関連する問題