RecyclerView中的NullPointException

时间:2016-04-14 08:56:14

标签: android-recyclerview

我尝试从AsyncTask中的服务器下载列表并将其放入recyclerView。但是,我仍然在RecyclerView上得到NullPointException并且不知道为什么。我设置LayoutManager和其他东西但问题仍然存在。 这是我的片段:

public class ChatMainFragment extends Fragment {

private static final String TAG = ChatMainFragment.class.getSimpleName() ;
private List<ChatUserListItem> userList;
private ArrayAdapter adapter;
public RecyclerView mUserListView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.chat_user_list, container, false);
    mUserListView = (RecyclerView)view.findViewById(R.id.user_list);
    GetUsersAsyncTask task = new GetUsersAsyncTask(this);
    task.execute();


    return view;
}



  public class GetUsersAsyncTask extends AsyncTask<String, String, List<ChatUserListItem>> {


   public Context mContext;
   private ChatMainFragment mFragment;

   public GetUsersAsyncTask(ChatMainFragment fragment) {
       this.mFragment = fragment;
   }


   @Override
   protected List<ChatUserListItem> doInBackground(String... params) {
       List<ChatUserListItem> userList = new ArrayList<>();
       try {
           userList  = ServerConnector.getInstance(mContext).getChatUserList();
        /*requestEnds(userList);*/
       } catch (ServerConnectorException e) {
           e.printStackTrace();
       }

       return userList;
   }

   @Override
   protected void onPostExecute(List<ChatUserListItem> chatUserListItems) {
       Log.e(TAG, "onPostExecute: ");
       userList = chatUserListItems ;
       ChatMainAdapter adapter = new ChatMainAdapter(getActivity().getApplicationContext(), userList);
       mUserListView.setAdapter(adapter);
       mUserListView.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext()));
       adapter.notifyDataSetChanged();


   }
   }
}

这是我的适配器:

public class ChatMainAdapter extends RecyclerView.Adapter<ChatMainAdapter.MyViewHolder> {
    private LayoutInflater inflater;
    private TextView mUserAvatar;

private Context mContext;
 List<ChatUserListItem> userList;

public ChatMainAdapter(Context context,List<ChatUserListItem>userList){
    this.mContext=context;
    this.userList=userList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int i) {
    View view = inflater.inflate(R.layout.chat_user_list_item, parent, false);
    MyViewHolder holder = new MyViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(MyViewHolder viewHolder, int position) {
        ChatUserListItem current = userList.get(position);
        viewHolder.mUserName.setText(current.getName());
}

@Override
public int getItemCount() {
    return userList.size();
}
class MyViewHolder extends RecyclerView.ViewHolder{

  TextView mUserName;

    public MyViewHolder(View itemView) {
        super(itemView);
        mUserName= (TextView)itemView.findViewById(R.id.user_name);

    }

}
}

我做错了什么?

1 个答案:

答案 0 :(得分:1)

像这样更改您的AsyncTask

                        public class GetUsersAsyncTask extends AsyncTask<String, String, List<ChatUserListItem>> {


                     public Context mContext;
                     private ChatMainFragment mFragment;
                     private RecyclerView mUserRecyclerView;

                     public GetUsersAsyncTask(ChatMainFragment fragment,RecyclerView mRListView) {
                         this.mFragment = fragment;
                         this.mUserRecyclerView=mRListView;
                     }


                     @Override
                     protected List<ChatUserListItem> doInBackground(String... params) {
                         List<ChatUserListItem> userList = new ArrayList<>();
                         try {
                             userList  = ServerConnector.getInstance(mContext).getChatUserList();
                          /*requestEnds(userList);*/
                         } catch (ServerConnectorException e) {
                             e.printStackTrace();
                         }

                         return userList;
                     }

                     @Override
                     protected void onPostExecute(List<ChatUserListItem> chatUserListItems) {
                         Log.e(TAG, "onPostExecute: ");
                         userList = chatUserListItems ;
                         ChatMainAdapter adapter = new ChatMainAdapter(getActivity().getApplicationContext(), userList);
                        mUserRecyclerView.setAdapter(adapter);
                        mUserRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext()));
                         adapter.notifyDataSetChanged();


                     }
                     }

当您在onCreateView中启动异步类时,就像这样启动它

 mUserListView = (RecyclerView)view.findViewById(R.id.user_list);
GetUsersAsyncTask task = new GetUsersAsyncTask(this,mUserListView);

只需替换代码,它应该按预期工作..希望这有帮助