简体   繁体   English

RecyclerView中的getItemcount上的空指针异常

[英]Null pointer Exception on getItemcount in recyclerView

I have been working on recyclerView but when i run my app it is not showing any data on the activity and app crashes and it give error on this method 我一直在使用recyclerView,但是当我运行我的应用程序时,它没有显示有关活动的任何数据,并且应用程序崩溃,并且此方法给出了错误

Here is my Adaptor class: 这是我的适配器类:

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();
}

} }

Here is my activity class: 这是我的活动课:

    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);


               }

Whenever i run it, it crashes and the error occur in getItemcount() method that it is null but i have initialize it to Info.size(); 每当我运行它时,它就会崩溃并且在getItemcount()方法中会发生错误,该方法为null,但是我已将其初始化为Info.size();。

In your processData function, you're creating an Info list, adding new items to it. processData函数中,您正在创建一个Info列表,并向其中添加新项目。 That's all good. 很好

However, after that you're creating a new adapter with that Info list, then you're setting that adapter again with the Info list. 但是,此后,您将使用该“ Info列表创建一个新的适配器,然后使用“ Info列表再次设置该适配器。

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

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

myadaptor.updateList(Info);

Perhaps that's causing issues with it becoming null. 也许这导致它变为null的问题。

okey .. i checked your code. 好的..我检查了您的代码。 For me there is no obvious reason for this exception but anyway let us fix and debug some issues. 对我来说,没有明显的例外原因,但是无论如何,让我们修复和调试一些问题。

before anything please change the name of you objects/class ... 在任何事情之前,请先更改您的对象/类的名称...

/// 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;
}

And test your program if the problem is still then let use test your project. 如果问题仍然存在,请测试您的程序,然后使用测试您的项目。

Firstly in your adapter replace this line private ArrayList<info> Info=new ArrayList<>(); 首先,在您的适配器中替换此行private ArrayList<info> Info=new ArrayList<>(); to private ArrayList<info> Info= null; private ArrayList<info> Info= null; .Then let us debug your code .. 然后让我们调试您的代码..

Firstly set these lines as a comment BackgroundHome backgroundHome=new BackgroundHome(this,this); backgroundHome.execute(); 首先将这些行设置为注释BackgroundHome backgroundHome=new BackgroundHome(this,this); backgroundHome.execute(); BackgroundHome backgroundHome=new BackgroundHome(this,this); backgroundHome.execute(); then run your program .. Mostly your program is going to run without error .. if this happened then your error in processData(JSONArray data) 然后运行您的程序..大多数情况下,您的程序将无错误运行..如果发生这种情况,那么您在processData(JSONArray data)错误

Then let us check your code and modify it. 然后,让我们检查您的代码并进行修改。 You do not need these lines . 您不需要这些行。

 myadaptor=new Adaptor(Info); // delete

 recyclerView.setAdapter(myadaptor);// delete

Now run your program .. 现在运行您的程序..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM