Estoy intentando crear mi vista personalizada a través de xml, pero la pantalla no muestra mi vista, s no está inflando. Mi vista personalizada XML es como:Vista personalizada sin inflar
<?xml version="1.0" encoding="utf-8"?>
<ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/profileSwitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/imgAdvertise"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@android:drawable/btn_minus" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<TextView
android:id="@+id/txtAdvertise"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="asdas" />
</LinearLayout>
</ViewSwitcher>
tengo mi clase personalizada View
como:
public class MyCustomView extends View{
TextView textAdvertise;
ImageView imageAdvertise;
View view;
ViewSwitcher switcher ;
public MyCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (layoutInflater != null) {
view = layoutInflater.inflate(R.layout.main, null);
}
initialize();
}
public void initialize() {
// TODO Auto-generated method stub
textAdvertise = (TextView) view.findViewById(R.id.txtAdvertise);
imageAdvertise = (ImageView) view.findViewById(R.id.imgAdvertise);
switcher = (ViewSwitcher) view.findViewById(R.id.profileSwitcher);
startAnimation();
}
public void startAnimation() {
// TODO Auto-generated method stub
new Thread() {
public void run() {
for (;;) {
try {
Thread.sleep(2000);
hRefresh.sendEmptyMessage(5);
} catch (Exception e) {
}
}
}
}.start();
}
Handler hRefresh = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case 5:
switcher.showNext();
// To go back to the first view, use switcher.showPrevious()
break;
default:
break;
}
}
};
}
y en mi diseño xml base donde tengo que mostrar el View
, es un botón y ya mi View
como:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:text="Button"
android:layout_weight="1"/>
<com.example.viewswitcher.MyCustomView
android:id="@+id/MyView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
Puede alguien por favor dime que esta mal?
Hola gracias por la ayuda .. Funcionó .. En realidad en mi mi inflador de diseño que he escrito view = layoutInflater.inflate (R.layout.main, null); este nulo causa un problema ... en vez puse "esto" Funcionó ... gracias amigo !! – NullPointerException
qué edición has hecho en cuestión ... y una cosa más funcionó porque extendí LinearLayout en vez de ver ... ¿por qué esto? Cualquier enlace o publicación relacionada con este detalle ... gracias – NullPointerException
@Rashmi He editado su pregunta para formatear mejor el código de la pregunta. La clase 'View' representa un único objeto' View', no acepta '' View's de los niños como si estuvieras tratando de agregarlo con el método 'inflate' (de hecho, si miras el método' inflate' documentos verá que el segundo parámetro es un 'ViewGroup'). En cambio, un 'ViewGroup' (' LinearLayout' es una subclase de 'ViewGroup') representa una' View' (de hecho es una subclase de la 'superclase 'View') que acepta' 'Views'' de los niños. No conozco ningún enlace, pero podría consultar los documentos para 'Ver' y 'Ver grupo'. – Luksprog