0

I'm initialing imageview at DailyCardView.java which are called by a fragment. But it keeps showing nullPointerException. I really don't know where went wrong... Guys help me><

I tried getRootView() to get view of Fragment so i can code:iv=(ImageView)getRootView().findViewById(R.id.iv); But it still not work.

**//Fragment**
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.home_fragment, container, false);
    dailyStackLayout = (DailyStackLayout)view.findViewById(R.id.dailyStackLayout);
    dailyStackLayout.setDatas(dailyFoodList);
    return view;
}

**//DailyStackLayout**
public DailyStackLayout(Context context) {
    this(context, null);
}
public DailyStackLayout(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}
public DailyStackLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

public void init() {
    params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    scaleY = DensityUtil.dip2px(getContext(), BASESCALE_Y_VALUE);
}
public void setDatas(List<DailyFood> dailyFoodList) {
    this.dailyFoodList = dailyFoodList;
    if (dailyFoodList == null) {
        return;
    }
    for (int i = index; index < i + STACK_SIZE; index++) {
        dc = new DailyCardView(getContext());
        dc.bind(dailyFoodList.get(index));
    }
}

**//DailyCardView**
public DailyCardView(Context context) {
    super(context, null);
}
public DailyCardView(Context context, AttributeSet attrs) {
    this(context, attrs,0);
}

public DailyCardView(Context context, AttributeSet attrs, int defStyleAttr) 
{
    super(context, attrs, defStyleAttr);
    init(context);
}

public void init(Context context) {
    if (!isInEditMode()) {
        inflate(context, R.layout.item_daily_card, this);
        iv = (ImageView)findViewById(R.id.iv);
    }
}
public void bind(DailyFood dailyFood) { 
    if (dailyFood == null) {return;}
    if (!TextUtils.isEmpty(dailyFood.getId())) {
        Glide.with(**iv.getContext()**)  // **NullPointerException**!
                .load(Constant.Daily_URL+dailyFood.getId()+".jpg")
                .into(iv);
}

I expect to load the image right but it went wrong at getContext().

1
  • iv is defined at R.layout.item_daily_card.
    – 郑安琪
    Jan 25, 2019 at 17:39

1 Answer 1

0
dc = new DailyCardView(getContext());

This calls the DailyCardView(Context) constructor that does not call init():

public DailyCardView(Context context) {
    super(context, null);
}

The init() call would init iv. And then you call bind() that assumes that iv is initialised. Bang, NPE.

Either change the super in the constructor to this to delegate to another constructor that calls init(), or just add a call to init().

1
  • thanks!!! Problem solved!! I'm really stupid... thanks!!!
    – 郑安琪
    Jan 26, 2019 at 4:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.