在充气

时间:2015-05-02 10:15:50

标签: android android-layout nullpointerexception android-custom-view layout-inflater

我有一个自定义RelativeLayout,我在其中填充了一个xml res文件。 如果我在xml文件中使用自定义布局并将其设置为contentview,这可以正常工作,但如果我尝试使用new LocationItem(this)addChild()在代码中添加它,则findViewById方法始终返回自定义null的构造函数中的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);
        }
    };
} ...
}

在活动中添加自定义视图

mRootLayoutLocations.addView(new LocationItem(this));

视图正确膨胀,因为我可以看到它,但是当我尝试访问自定义视图中的视图时,应用程序崩溃并出现NullPointerException。

4 个答案:

答案 0 :(得分:1)

您需要使用适当的构造函数,而不是重载它们。

public class LocationItem extends RelativeLayout {

    private String parcelType;
    private int countIntoBox, countFromBox;

    private RelativeLayout deliveryContainer, pickupContainer;

    private TextView countPickup, countDelivery;

    public LocationItem(Context context) {
        super(context);
        init(context, null, 0);
    }

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

    public LocationItem(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs, defStyle);
    }

    private void init(Context context, AttributeSet attrs, int 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);
            }
        };
    }

    ...
}

答案 1 :(得分:0)

我担心你必须给视图充气,而不是从任何地方找到它。方法

findindViewById(int Id)

必须在Actvity的onCreate中调用,或者在您尝试查找的子视图/小部件所在的视图中调用。

如果所有子视图都驻留在单个xml文件中(在单父根中)

View rootView=(View) LayoutInflater.from(context).inflate(R.layout.list_item_location);
pickupContainer = (RelativeLayout) rootview.findViewById(R.id.rl_location_pickup_container);

答案 2 :(得分:0)

这应该有效

public LocationItem(Context context, AttributeSet attrs, int defStyle) {
   super(context, attrs, defStyle);
   this = inflate(getContext(), R.layout.list_item_location,null);
   ...

答案 3 :(得分:0)

您可以尝试一下。 inFinishInflate()方法中的init视图。在添加所有子视图之后,该方法将称为通货膨胀的最后阶段。因此可以避免使用NPE。