我已经实施了cardview以及recycleview。我有一个自定义适配器,现在我想更改字体。我尝试使用:
private Typeface custom_font = Typeface.createFromAsset(context.getAssets(), "fonts/VarelaRound-Regular.ttf");
但是它给出了错误跟踪错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.AssetManager android.content.Context.getAssets()' on a null object reference
当我尝试将背景颜色设置为文本视图时也出现错误
holder.count.setBackgroundColor(context.getResources().getColor(R.color.software_color));
我已将自定义适配器代码附加到
下面public class CardViewAdapter extends RecyclerView.Adapter<CardViewAdapter.MyViewHolder> {
private ArrayList<CardViewData> cardViewDataSet;
public Resources res;
public static class MyViewHolder extends RecyclerView.ViewHolder {
protected TextView title;
protected TextView count;
public MyViewHolder(View itemView) {
super(itemView);
this.title = (TextView) itemView.findViewById(R.id.title);
this.count = (TextView) itemView.findViewById(R.id.count);
}
}
public CardViewAdapter(ArrayList<CardViewData> cardviewdata) {
this.cardViewDataSet = cardviewdata;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.card_layout, parent, false);
// view.setOnClickListener(MainActivity.myOnClickListener);
MyViewHolder myViewHolder = new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int listPosition) {
Typeface custom_font = Typeface.createFromAsset(context.getAssets(), "fonts/VarelaRound-Regular.ttf");
holder.title.setText((String)cardViewDataSet.get(listPosition).getTitle());
holder.count.setText(String.valueOf(cardViewDataSet.get(listPosition).geCount()));
holder.title.setTypeface(custom_font);
holder.count.setTypeface(custom_font);
holder.count.setBackgroundColor(context.getResources().getColor(R.color.software_color));
}
@Override
public int getItemCount() {
return cardViewDataSet.size();
}
}