【问题标题】:Null pointer Exception on getItemcount in recyclerViewrecyclerView 中 getItemcount 的空指针异常
【发布时间】:2017-09-01 21:18:00
【问题描述】:

我一直在研究 recyclerView,但是当我运行我的应用程序时,它没有显示任何有关活动的数据,并且应用程序崩溃,并且此方法出现错误

这是我的适配器类:

public class Adaptor extends RecyclerView.Adapter<Adaptor.ViewHolder> {


private  ArrayList<info> Info=new ArrayList<>();

public Adaptor(ArrayList<info> info) {
    this.Info = info;
}


public void updateList(ArrayList<info> data) {
    Info = data;
    notifyDataSetChanged();
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.list_view, parent, false);

         return new ViewHolder(itemView);


}



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

    info i= Info.get(position);
    holder.name.setText(i.getName());
    holder.city.setText(i.getCity());
    holder.city.setText(i.getUniversity());


}



public class ViewHolder extends RecyclerView.ViewHolder {

    TextView name, city, uni;


    public ViewHolder(final View v) {
        super(v);
        name = (TextView) v.findViewById(R.id.name);
        city = (TextView) v.findViewById(R.id.city);
        uni = (TextView) v.findViewById(R.id.university);

    }




}

public int getItemCount() {
    return Info.size();
}

}

这是我的活动课:

    public class Home extends AppCompatActivity implements HomeCallBack {

    public ArrayList<info> Info;
    private  Adaptor myadaptor;
private RecyclerView recyclerView;
private RecyclerView.LayoutManager layoutManager;



@Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    recyclerView=(RecyclerView)findViewById(R.id.recycle_view);
      Info=new ArrayList<>();

    myadaptor=new Adaptor(Info);
    recyclerView.addItemDecoration(new DividerItemDecoration(this, LinearLayoutManager.VERTICAL));
    LinearLayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(myadaptor);



    BackgroundHome backgroundHome=new BackgroundHome(this,this);
    backgroundHome.execute();


}

@Override
public void processData(JSONArray data)
{

    JSONObject js;

            if(data!=null)
            {
                Info=new ArrayList<>();


                try
                {
                    for(int i=0;i<data.length();i++)
                    {
                        js =data.getJSONObject(i);
                        info in=new 


  info(js.getString("Names"),js.getString("city"),
  js.getString("university"));
                        Info.add( in);

                    }


                    myadaptor=new Adaptor(Info);

                    recyclerView.setAdapter(myadaptor);

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

                myadaptor.updateList(Info);


               }

每当我运行它时,它都会崩溃,并且 getItemcount() 方法中发生错误,它为 null 但我已将其初始化为 Info.size();

【问题讨论】:

标签: android json android-recyclerview


【解决方案1】:

在您的 processData 函数中,您正在创建一个 Info 列表,并向其中添加新项目。这一切都很好。

但是,在您使用 Info 列表创建新适配器之后,您将使用 Info 列表再次设置该适配器。

myadaptor =new Adaptor(Info);
recyclerView.setAdapter(myadaptor);

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

myadaptor.updateList(Info);

也许这会导致它变为 null。

【讨论】:

  • 他不需要这些行是的,但我认为这不是问题所在。如果您考虑一下他的所作所为..他将新的适配器对象设置为他的回收视图..只是对象不再更改..然后他在此行myadaptor.updateList(Info);中再次设置了他在其中创建的列表,也没有问题与那个
【解决方案2】:

好的 .. 我检查了你的代码。对我来说,这个异常没有明显的原因,但无论如何让我们修复和调试一些问题。

在任何事情之前,请更改您的对象/类的名称......

/// you have class with name 'info'
private  ArrayList<info> Info=new ArrayList<>();
// here you set variable with the same class name !!! 
public Adaptor(ArrayList<info> info) {
  this.Info = info;
}

如果问题仍然存在,请测试您的程序 然后让我们测试你的项目。

首先在您的适配器中将这一行private ArrayList&lt;info&gt; Info=new ArrayList&lt;&gt;(); 替换为private ArrayList&lt;info&gt; Info= null;。然后让我们调试您的代码..

首先将这些行设置为注释BackgroundHome backgroundHome=new BackgroundHome(this,this); backgroundHome.execute();,然后运行您的程序..大多数情况下您的程序将正常运行..如果发生这种情况,那么您在processData(JSONArray data)中的错误

然后让我们检查您的代码并进行修改。 你不需要这些行。

 myadaptor=new Adaptor(Info); // delete

 recyclerView.setAdapter(myadaptor);// delete

现在运行你的程序..

【讨论】:

  • 我的动机不是在不崩溃的情况下运行应用程序,而是显示活动数据..
  • 在我完成了你告诉我的所有操作后,应用程序正在运行,但没有显示任何内容!
  • okey .. 空错误消失了!!!我预计这个结果没问题,我可以告诉你下一步是什么,但首先我需要你做一些事情......在myadaptor.updateList(Info);下方设置日志,并在此日志中显示信息列表的大小。在返回之前还设置登录getItemCount() 并检查信息列表的大小并告诉我结果
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-27
  • 2019-11-17
  • 2013-12-19
  • 2017-04-18
相关资源
最近更新 更多