4

我有一個自定義RelativeLayout,我膨脹一個XML文件中的res文件。 這工作得很好,如果我在一個XML文件中使用自定義佈局,並將其設置爲內容查看,但如果我嘗試將其與new LocationItem(this)addChild()添加在代碼中findViewById方法總是在自定義RelativeLayout的構造函數返回nullfindViewById在自定義視圖中返回null後膨脹

下面是代碼:

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自定義查看應用程序崩潰。

+0

您的問題是mRootLayoutLocations爲空。這不在這個類的定義中。 – torque203

+0

絕對不是! mRootLayoutLocations不爲null!此佈局位於我添加LocationItems的活動中:) – Fabian

回答

0

您需要使用適當的構造函數,而不是將它們重載。

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); 
      } 
     }; 
    } 

    ... 
} 
+0

相同。仍然是NullPointerException。 – Fabian

+1

請發佈您的堆棧跟蹤...您是否還要設置代碼中的寬度和高度的必填字段? :) –

+2

好吧我膨脹視圖到一個視圖(持有人) 查看v = inflate(getContext(),R.layout.list_item_location,this); ,然後通過v.findViewById訪問視圖。 現在工作。也許這個視圖沒有太快的附加到rootview? – Fabian

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); 
+1

這就是我所做的 - 見下面的評論。 – Fabian

0

這應該工作

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

確定我充氣視圖到視圖(保持器)

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

,然後經由v.findViewById訪問視圖。現在它正在工作。

代碼:

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());