Reputation: 1037
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 other stuff but problem still exists. Here's my fragment:
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();
}
}
}
Here's my adapter :
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);
}
}
}
What I'm doing wrong ?
Upvotes: 0
Views: 38
Reputation: 1114
Change Your AsyncTask Like This
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();
}
}
and when you initiate your async class in onCreateView initiate it like this
mUserListView = (RecyclerView)view.findViewById(R.id.user_list);
GetUsersAsyncTask task = new GetUsersAsyncTask(this,mUserListView);
Just replace the code and it should work as expected .. Hope this helps
Upvotes: 1