上下文-空对象引用

时间:2018-07-23 18:25:21

标签: android android-recyclerview tablelayout android-viewholder

我有带有RecyclerView适配器的片段。 我也有月份数组和隐藏帖子数组对象。

我尝试动态添加帖子表行。我不知道应该怎么做。

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.NewsCardViewHolder> {

private List<NewsMonth> months;
private List<NewsPost> posts;
private AdapterView.OnItemClickListener onItemClickListener;
private String TAG = "NewsAdapter";
private Context _context;
TableLayout tableLayout;

public NewsAdapter(List<NewsMonth> months, Context context) {
    this.months = months;
    this._context = context;
}

class NewsCardViewHolder extends RecyclerView.ViewHolder {
    CardView cv;
    TextView monthName;
    TextView postLabel;
    TextView postType;
    TableLayout tableLayout;

    NewsCardViewHolder(View itemView) {
        super(itemView);
        cv = itemView.findViewById(R.id.cv);
        monthName = itemView.findViewById(R.id.month_name);
        postLabel = itemView.findViewById(R.id.news_label);
        postType = itemView.findViewById(R.id.news_category);
        tableLayout = itemView.findViewById(R.id.tableNews);
    }
}

@NonNull
@Override
public NewsCardViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.news_item, viewGroup, false);
    return new NewsCardViewHolder(v);
}

@Override
public void onBindViewHolder(@NonNull NewsCardViewHolder ViewHolder, final int position) {
    ViewHolder.monthName.setText(months.get(position).getMonthName());
    posts = months.get(position).getPosts();
    for (int j = 0; j < posts.size(); j++) {
        //ViewHolder.itemView.addView(months.get(position).getPosts().get(j).getTitle());
        TextView postLabel = new TextView(_context.getApplicationContext());
        postLabel.setGravity(Gravity.START);
        postLabel.setText(months.get(position).getPosts().get(j).getTitle());
        postLabel.setLayoutParams(new TableRow.LayoutParams
                (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        postLabel.setTextSize(10);
        postLabel.setTextColor(Color.parseColor("#FF9900"));
        TableRow row = new TableRow(_context.getApplicationContext());
        row.addView(postLabel);
        tableLayout.addView(row);
    }
}

在活动开始时,我得到: java.lang.NullPointerException:尝试在此空对象引用上调用虚拟方法'android.content.Context android.content.Context.getApplicationContext()'行: TextView postLabel =新的TextView ...

编辑:

public class NewsFragment extends Fragment{

private List<NewsMonth> monthArray;
private List<NewsPost> postArray;
private TableLayout tableLayout;

public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.news_fragment, container, false);
}

@Override
public void onViewCreated(@NonNull View view,
                          Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    ViewPager viewPager = view.findViewById(R.id.view_pager);
    tableLayout = view.findViewById(R.id.tableNews);
    ImageSliderAdapter sliderAdapter = new ImageSliderAdapter(getContext());
    viewPager.setAdapter(sliderAdapter);
    initializeData();
    RecyclerView recyclerView = view.findViewById(R.id.newsList);
    recyclerView.setNestedScrollingEnabled(false);
    recyclerView.setHasFixedSize(false);
    recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity())); //getActivity(), LinearLayoutManager.VERTICAL, false
    NewsAdapter adapter = new NewsAdapter(monthArray, getContext());
    recyclerView.setAdapter(adapter);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
}

1 个答案:

答案 0 :(得分:2)

我认为您需要正确设置_context。将活动中的上下文传递到适配器中,然后将_context设置为要传递的上下文。

public NewsAdapter(List<NewsMonth> months, Context context) {
    this.months = months;
    _context = context;
}