Nullpointexception In Recyclerview
I try to download list from server in AsyncTask and put it into recyclerView. However, I still get NullPointException on RecyclerView and have no idea why. I set LayoutManager and
Solution 1:
Change Your AsyncTask Like This
publicclassGetUsersAsyncTaskextendsAsyncTask<String, String, List<ChatUserListItem>> {
publicContext mContext;
privateChatMainFragment mFragment;
privateRecyclerView mUserRecyclerView;
publicGetUsersAsyncTask(ChatMainFragment fragment,RecyclerView mRListView) {
this.mFragment = fragment;
this.mUserRecyclerView=mRListView;
}
@OverrideprotectedList<ChatUserListItem> doInBackground(String... params) {
List<ChatUserListItem> userList = newArrayList<>();
try {
userList = ServerConnector.getInstance(mContext).getChatUserList();
/*requestEnds(userList);*/
} catch (ServerConnectorException e) {
e.printStackTrace();
}
return userList;
}
@OverrideprotectedvoidonPostExecute(List<ChatUserListItem> chatUserListItems) {
Log.e(TAG, "onPostExecute: ");
userList = chatUserListItems ;
ChatMainAdapter adapter = newChatMainAdapter(getActivity().getApplicationContext(), userList);
mUserRecyclerView.setAdapter(adapter);
mUserRecyclerView.setLayoutManager(newLinearLayoutManager(getActivity().getApplicationContext()));
adapter.notifyDataSetChanged();
}
}
and when you initiate your async class in onCreateView initiate it like this
mUserListView = (RecyclerView)view.findViewById(R.id.user_list);
GetUsersAsyncTasktask=newGetUsersAsyncTask(this,mUserListView);
Just replace the code and it should work as expected .. Hope this helps
Post a Comment for "Nullpointexception In Recyclerview"