在膨胀后,findViewById在自定义视图中返回null [英] findViewById returns null in custom View after inflate

查看:106
本文介绍了在膨胀后,findViewById在自定义视图中返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义RelativeLayout,并在其中添加了一个xml res文件. 如果我在xml文件中使用自定义布局并将其设置为contentview,这将很好地工作,但是如果我尝试使用new LocationItem(this)addChild()将其添加到代码中,则findViewById方法始终在构造函数中返回null自定义RelativeLayout

I have a custom RelativeLayout and I inflate an xml res file in it. This works fine if I use the custom layout in an xml file and set it as contentview, but if I try to add it in the code with new LocationItem(this) and addChild() the findViewById method always returns null in the constructor of the custom RelativeLayout.

这是代码:

public class LocationItem extends RelativeLayout {

private String parcelType;
private int countIntoBox, countFromBox;

private RelativeLayout deliveryContainer, pickupContainer;

private TextView countPickup, countDelivery;

public LocationItem(Context context) {
    this(context, null);
}

public LocationItem(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public LocationItem(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    inflate(getContext(), R.layout.list_item_location, this);
    deliveryContainer = (RelativeLayout) findViewById(R.id.rl_location_delivery_container);
    pickupContainer = (RelativeLayout) findViewById(R.id.rl_location_pickup_container);
    countPickup = (TextView) findViewById(R.id.tv_location_pickup_count);
    countDelivery = (TextView) findViewById(R.id.tv_location_delivery_count);

    countPickup.setOnClickListener(getShowNumberPickerListener());
    countDelivery.setOnClickListener(getShowNumberPickerListener());
}

private OnClickListener getShowNumberPickerListener() {
    return new OnClickListener() {
        @Override
        public void onClick(View view) {
            showNumberPickerDialog(view);
        }
    };
} ...
}

在活动中添加自定义视图

Add custom view in activity

mRootLayoutLocations.addView(new LocationItem(this));

该视图已正确膨胀,因为可以看到它,但是当我尝试访问自定义视图内的视图时,应用程序崩溃,并显示NullPointerException.

The view is inflated correctly, because I can see it, but when I try to access a view inside the custom view the app crashes with a NullPointerException.

推荐答案

好吧,我将视图放大为View(holder)

Ok i inflated the view into a View(holder)

View v = inflate(getContext(), R.layout.list_item_location, this); 

,然后通过v.findViewById访问视图.现在可以了.

and then access the views via v.findViewById. Now it's working.

代码:

View v = inflate(getContext(), R.layout.list_item_location, this);
deliveryContainer = (RelativeLayout) v.findViewById(R.id.rl_location_delivery_container);
pickupContainer = (RelativeLayout) v.findViewById(R.id.rl_location_pickup_container);
countPickup = (TextView) v.findViewById(R.id.tv_location_pickup_count);
countDelivery = (TextView) v.findViewById(R.id.tv_location_delivery_count);

countPickup.setOnClickListener(getShowNumberPickerListener());
countDelivery.setOnClickListener(getShowNumberPickerListener());

这篇关于在膨胀后,findViewById在自定义视图中返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆