适配器notifyDataSetChanged()上的NullPointerException。我需要做什么?

时间:2016-02-26 03:12:16

标签: android nullpointerexception android-recyclerview recycler-adapter

所以我有一个AsyncTask类来处理填充我的RecyclerView。它运行良好但是当它需要刷新时它会崩溃NullPointerException。我知道为什么然后我不能这样做,因为我正在创建一个新的object并将其添加到我的list

这是我的代码:

  @Override
        protected void onPostExecute(String s) {
            try {
                userslist.clear();
                JSONArray jsonArray = new JSONArray(s);
                for(int i=0; i < jsonArray.length(); i++){
                    ...
                    UsersData usersData = new UsersData(var1, var2);
                    userslist.add(UsersData);
                }
                cAdapter.notifyDataSetChanged(); // Culprit line, despite the list being deleted and added again in the try block
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

我怀疑它与userslist有关。

这是我的错误日志:

java.lang.NullPointerException
            at lukazs.newapp.UserInfo$GetUserList.onPostExecute(UserInfo.java:218)
            at android.os.AsyncTask.finish(AsyncTask.java:741)
            at android.os.AsyncTask.access$600(AsyncTask.java:197)
            at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:654)
            at android.os.Handler.dispatchMessage(Handler.java:100)

编辑:这是方法调用,我填充recyclerview

public void populate(){
    GetUserList getUserList = new GetUserList();
    getUserList.execute();
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    ...
    populate();
    }

这是cAdapter方法中初始化onCreate的地方:

RecyclerView.Adapter cAdapter;
ArrayList<UserDetailsProvider> userslist = new ArrayList<UserDetailsProvider>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    populateRecyclerList();
    recyclerView = (RecyclerView) findViewById(R.id.recycleMusic);
    cAdapter = new UserDefinedAdapter(userslist);
    recyclerView.setAdapter(cAdapter);
}

2 个答案:

答案 0 :(得分:1)

我的适配器模式,

public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
       private List<YOUR CLASS> data = new ArrayList<>();
       Context context;

       public MyAdapter(Context context){
             this.context = context;
       }

       public void addAll(List<YOUR CLASS> data) {
             this.data.addAll(data);
             this.notifyDataSetChanged();
       }

       public void clear() {
             data.clear();
             this.notifyDataSetChanged();
       }
           //Other Methods of Adapter
}

在你的onCreate()方法中,只需将适配器初始化为recyclerview。

cAdapter = new MyAdapter(this);
recyclerView.setAdapter(cAdapter);

in asynctask,

@Override
    protected void onPostExecute(String s) {
        try {
            cAdapter.clear();
            JSONArray jsonArray = new JSONArray(s);
            for(int i=0; i < jsonArray.length(); i++){
                ...
                UsersData usersData = new UsersData(var1, var2);
                userslist.add(usersData);
            }
            cAdapter.addAll(userslist); 
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

答案 1 :(得分:0)

首先从listview获取适配器然后更新

ListAdapter cAdapter=listview.getAdapter();
cAdapter.notifyDataSetChanged();