FindViewById en una vista personalizada para encontrar la vista secundaria

He definido una vista, se extiende RelativeLayout , Quiero extraer a sus hijos cuyo ID se especifican en el archivo xml de diseño.

En HeaderView.java :

  1 package com.example.test; 2 3 public class HeaderView extends RelativeLayout { 4 5 public HeaderView(Context context, AttributeSet attrs) { 6 this(context, attrs, 0); 7 8 TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.HeaderView, 0, 0); 9 10 int headerId = 0; 11 int containerId = 0; 12 13 // find attributes 14 if (arr != null) { 15 if (arr.hasValue(R.styleable.HeaderView_headerview_header)) { 16 headerId = arr.getResourceId(R.styleable.HeaderView_headerview_header, 0); 17 } 18 if (arr.hasValue(R.styleable.HeaderView_headerview_container)) { 19 containerId = arr.getResourceId(R.styleable.HeaderView_headerview_container, 0); 20 } 21 arr.recycle(); 22 } 23 24 Log.d("test", String.format("headerId: %s, containerId %s", headerId, containerId)); 25 // headerId: 2131296260, containerId 2131296259 26 27 // here: mHeaderContainer is null 28 mHeaderContainer = (RelativeLayout) findViewById(headerId); 29 mContentViewContainer = (RelativeLayout) findViewById(containerId); 30 } 31 } 

He definido su atributo en headview_attrs.xml :

 <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="HeaderView"> <attr name="headerview_header" format="reference" /> <attr name="headerview_container" format="reference" /> </declare-styleable> </resources> 

Utilizo esta vista en un archivo xml de diseño, en activity_headerview.xml :

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:test="http://schemas.android.com/apk/res/com.example.test" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.test.HeaderView android:layout_width="match_parent" android:layout_height="match_parent" test:headerview_container="@+id/headerview_content" test:headerview_header="@+id/headerview_header" > <RelativeLayout android:id="@+id/headerview_header" android:layout_width="match_parent" android:layout_height="30dp" > </RelativeLayout> <RelativeLayout android:id="@+id/headerview_content" android:layout_width="match_parent" android:layout_height="match_parent" > </RelativeLayout> </com.example.test.HeaderView> </RelativeLayout> 

Aquí el problema viene: en HeaderView.java, line 28, 29, mHeaderContainer y mContentViewContainer son nulos.

Parece que en activity_headerview.xml , RelativeLayout cuyo id es headerview_header y RelativeLayout cuyo id es headerview_content no son los hijos de HeaderView .

¿Hice algo mal? ¡Cualquier ayuda será grande!

Cuando se creó su vista personalizada, la vista secundaria no se ha adjuntado a su vista. Intenta mover el código al método onFinishInflate () . Este método será invocado por LayoutInflater después de que todo el diseño haya sido inflado.

  • Android: ¿Cómo usar mediaController en la clase de servicio?
  • Cómo hacer un findViewById (R.id. >> StringVarHere <<)?
  • FindViewById dentro del fragmento
  • Android findViewById () en la vista personalizada
  • FindViewById dentro de un método estático
  • (Barra de herramientas) findViewById (R.id.tool_bar) devuelve NULL
  • Excepción de conversión de clase a la misma clase en Android
  • FindViewById () devuelve null - intentado TODO
  • El método findViewById (int) no está definido para el tipo Get Data Fragment for Accelerometer Sensor
  • Puntero nulo Excepción - findViewById ()
  • Android: ¿cómo puedo acceder a las vistas (utilizando findViewById) de una actividad de un fragmento onCreateView ()?
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.