我正在线上NullPointerException
:
FragmentManager fragmentManager = ((ProductActivity)context).getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
上下文返回null
,当我点击替换下面适配器中的片段时,它就崩溃了。
这是我的适配器:
public class ProductActivityAdapter extends RecyclerView.Adapter<ProductActivityAdapter.ViewHolder> {
private List<Product> products;
private int rowLayout;
public Context mContext;
public int position;
public ProductActivityAdapter(List<Product> products, int rowLayout, Context context) {
this.products = products;
this.rowLayout = rowLayout;
this.mContext = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(rowLayout, viewGroup, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int i) {
Product product = products.get(i);
viewHolder.card.setTag(i);
viewHolder.countryName.setText(product.name);
if(android.os.Build.VERSION.SDK_INT >= 21){
// viewHolder.countryImage.setImageDrawable(mContext.getDrawable(product.getImageResourceId(mContext)));
viewHolder.iconImage.setImageDrawable(mContext.getDrawable(product.getIconImage(mContext)));
} else {
// viewHolder.countryImage.setImageDrawable(mContext.getResources().getDrawable(product.getImageResourceId(mContext)));
viewHolder.iconImage.setImageDrawable(mContext.getResources().getDrawable(product.getIconImage(mContext)));
}
}
@Override
public int getItemCount() {
return products == null ? 0 : products.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView countryName;
public ImageView countryImage;
public ImageView iconImage;
public CardView card;
public Context context;
public Context getContext() {
return context;
}
public ViewHolder(View itemView) {
super(itemView);
countryName = (TextView) itemView.findViewById(R.id.countryName);
// countryImage = (ImageView)itemView.findViewById(R.id.countryImage);
iconImage = (ImageView) itemView.findViewById(R.id.iconImage);
card = (CardView) itemView.findViewById(R.id.card);
card.setOnClickListener(new CardClickListener());
}
private class CardClickListener implements CardView.OnClickListener {
@Override
public void onClick(View v) {
// Log.e("VIEW", "position=" + position);
int position = (int) v.getTag();
Log.e("VIEW", "position=" + position);
cardview(position);
}
}
public void cardview(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new emi_calculator_fragment();
break;
case 1:
Log.e("Position", "HI");
break;
case 2:
Log.e("Position", "HI");
break;
case 3:
Log.e("Position", "HI");
break;
case 4:
Log.e("Position", "HI");
break;
case 5:
Log.e("Position", "HI");
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = ((ProductActivity)context).getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
} else {
// error in creating fragment
Log.e("Product Activity", "Error in creating fragment");
}
}
}
}
答案 0 :(得分:2)
使用
mContext.getSupportFragmentManager();
而不是
((ProductActivity)context).getSupportFragmentManager();
您已将Context
定义为mContext
。
答案 1 :(得分:1)
在ViewHolder类中,您没有设置上下文,因此它为null。
public ViewHolder(Context context, View itemView) {
super(itemView);
this.context = context;
...
}