簡體   English   中英

RecyclerView中的getItemcount上的空指針異常

[英]Null pointer Exception on getItemcount in recyclerView

我一直在使用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();。

processData函數中,您正在創建一個Info列表,並向其中添加新項目。 很好

但是,此后,您將使用該“ Info列表創建一個新的適配器,然后使用“ Info列表再次設置該適配器。

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

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

myadaptor.updateList(Info);

也許這導致它變為null的問題。

好的..我檢查了您的代碼。 對我來說,沒有明顯的例外原因,但是無論如何,讓我們修復和調試一些問題。

在任何事情之前,請先更改您的對象/類的名稱...

/// 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<info> Info=new ArrayList<>(); private ArrayList<info> Info= null; 然后讓我們調試您的代碼..

首先將這些行設置為注釋BackgroundHome backgroundHome=new BackgroundHome(this,this); backgroundHome.execute(); BackgroundHome backgroundHome=new BackgroundHome(this,this); backgroundHome.execute(); 然后運行您的程序..大多數情況下,您的程序將無錯誤運行..如果發生這種情況,那么您在processData(JSONArray data)錯誤

然后,讓我們檢查您的代碼並進行修改。 您不需要這些行。

 myadaptor=new Adaptor(Info); // delete

 recyclerView.setAdapter(myadaptor);// delete

現在運行您的程序..

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM