方法RecyclerView.setLayoutManager处的空指针异常

时间:2017-07-27 03:54:28

标签: java android nullpointerexception android-recyclerview

我遇到了RecyclerViewAdapter的问题

以下是我的错误:

  

java.lang.NullPointerException:尝试调用虚方法'void   android.support.v7.widget.RecyclerView.setLayoutManager(安德烈oid.support.v7.widge t.RecyclerView $辣油tManager)'

在线:

  

recyclerView.setLayoutManager(mLayoutManager);

我在创建片段时设置了适配器recyclelerview,当用户点击recyclerview中的某个项目时,它会调用api来更新此recyclelerview,但问题是当用户点击异常上方的项目时

如何修复

以下是我的代码:

 recyclerView = (RecyclerView) lay_custom_center_post_topic.findViewById(R.id.lv_poll_feed);

 AdapDetailTopicPoll adapDetailTopicPoll = new AdapDetailTopicPoll(timelineFeedListModels.get(i).getPost().getPoll());
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapDetailTopicPoll);
adapDetailTopicPoll.notifyDataSetChanged();

new_position = i;
 public void sendVodePoll(final int position, String poll_id,final int new_position, final List<ProfileFeedListModelTwo.PostBean.PollBean> poll) {

        RequestParams params = new RequestParams();
        String URL = "xxxx/xx";
        params.put("poll_id", poll_id);

        AsyncHttpClient client = new AsyncHttpClient();
        client.post(URL, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
                ArrayList<ProfileFeedListModelTwo.PostBean.PollBean> pollBeanArrayList = new ArrayList<ProfileFeedListModelTwo.PostBean.PollBean>();
                StringBuilder result = new StringBuilder();
                String vote = null;
                String poll_id=null;
                String status_vote=null;
                Gson gson = new Gson();
                try {
                    List<TopicVoteModel> topicVoteModels = new ArrayList<>();
                    for (int i=0 ; i<response.length() ; i++){
                        JSONObject jsonObject = response.getJSONObject(i);
                        vote = jsonObject.getString("vote");
                        topicVoteModels.add(gson.fromJson(String.valueOf(jsonObject), TopicVoteModel.class));
                    }

                    boolean new_status_vote = Boolean.parseBoolean(status_vote);

                    poll.get(position).setVote(Integer.parseInt(vote));
                    poll.get(position).setStatus_vote(new_status_vote);

                    for (ProfileFeedListModelTwo.PostBean.PollBean pollBean : poll){
                        pollBeanArrayList.add(pollBean);
                    }

                    adapDetailTopicPoll = new AdapDetailTopicPoll(pollBeanArrayList, context);
                    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
                    recyclerView.setLayoutManager(mLayoutManager); //inline this error
                    recyclerView.setItemAnimator(new DefaultItemAnimator());
                    recyclerView.setAdapter(adapDetailTopicPoll);
                    adapDetailTopicPoll.notifyDataSetChanged();

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

适配器

public class AdapDetailTopicPoll extends RecyclerView.Adapter<AdapDetailTopicPoll.MyViewHolder> {

    SearchDetailFragment searchDetailFragment = new SearchDetailFragment();
    Context context;
    private List<ProfileFeedListModelTwo.PostBean.PollBean> poll = new ArrayList<ProfileFeedListModelTwo.PostBean.PollBean>();

    public AdapDetailTopicPoll( List<ProfileFeedListModelTwo.PostBean.PollBean> poll ,Context context){
        this.poll = poll;
        this.context=context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.custom_profile_feed_topic_poll,parent,false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {

        holder.tx_poll.setText(""+poll.get(position).getName());
        if (poll.get(position).isStatus_vote()==false){
            holder.img_vote.setImageResource(R.drawable.vote_t_1);
            holder.tx_count_vote.setText(""+poll.get(position).getVote());

        }else {
            holder.img_vote.setImageResource(R.drawable.vote_t_2);
            holder.tx_count_vote.setText(""+poll.get(position).getVote());

        }


    }

    @Override
    public int getItemCount() {
        return poll.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView tx_poll,tx_count_vote;
        public ImageView img_vote;

        public MyViewHolder(View view) {
            super(view);
            tx_poll = (TextView) view.findViewById(R.id.tx_poll);
            tx_count_vote = (TextView) view.findViewById(R.id.tx_count_vote);
            img_vote = (ImageView) view.findViewById(R.id.img_vote);

            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ProfileFragment.poll_id = String.valueOf(poll.get(getAdapterPosition()).getId());
                    searchDetailFragment.sendVodePoll(getAdapterPosition(),ProfileFragment.poll_id,SearchDetailFragment.new_position,poll);

                }
            });
        }
    }
}

2 个答案:

答案 0 :(得分:0)

将这些行直接写入活动的onCreate()方法

adapDetailTopicPoll = new AdapDetailTopicPoll(pollBeanArrayList, context);
RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapDetailTopicPoll);

并初始化适配器和arraylist作为全局变量。

答案 1 :(得分:-2)

在RecyclerView中,有两种常用的方式 - 以List的形式或以Grid的形式。因此,对于列表形成,您将提供这样的布局管理器 -

 LinearLayoutManager layoutManagerFuture = new LinearLayoutManager(context);
    layoutManagerFuture.setOrientation(LinearLayoutManager.VERTICAL);

和网格形成

GridLayoutManager layoutManager = new GridLayoutManager(context, totalItemsInOneRow);
    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);