Problem with RecyclerView and List on Android

1

Good morning.

I'm trying to download a series of data from the internet. To do this, through a webservice filled a list of objects. This list is sent to RecycleAdapter to show them in RecyclerView . But in doing so, I get this problem.

The fact is that this list IS full, and I do not know what can happen. The line where it says that this error is here.

Another curious thing is that when I start the application, at first if it starts to show the list, but at the second and little disappears and crashing showing the error that I have taught you before.

YEAR:

public void cargarBatallas(){
        final LinearLayoutManager layoutManager = new LinearLayoutManager(ctx);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        RecyclerView rviewtimeline = (RecyclerView)this.findViewById(R.id.recyclerView);
        rviewtimeline.setLayoutManager(layoutManager);
        //sTextView txtEmpty = (TextView)findViewById(R.id.emptyTimeline);
        //listTimeLine.setEmptyView(txtEmpty);
        List<Batalla> batallaList = new ArrayList<Batalla>();
        BatallasRecyclerAdapter recyclerAdapter = new BatallasRecyclerAdapter(batallaList);
        rviewtimeline.setAdapter(recyclerAdapter);
        GetBatallas getAsync = new GetBatallas(this, key_session, rviewtimeline , GetBatallas.GET_TIMELINE, null, recyclerAdapter);
        getAsync.execute();
    }

OnPostExecute (After downloading data and filling list)

@Override
protected void onPostExecute(Object result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);

    Log.d(msg, "probando");

    if(batallaList != null){
        Log.d(msg, "probando1");

        adapterRecycler.setData(batallaList);
        adapterRecycler.notifyDataSetChanged();}}

Adapter class:

public class BatallasRecyclerAdapter extends RecyclerView.Adapter<BatallasRecyclerAdapter.BatallaViewHolder>{


    List<Batalla> listBatalla;

    public BatallasRecyclerAdapter(List<Batalla> listBatalla){
        this.listBatalla = listBatalla;
    }

    public void setData(List<Batalla> listBatalla){
        this.listBatalla = listBatalla;
    }

    @Override
    public int getItemCount() {
        // TODO Auto-generated method stub
        return listBatalla.size();
    }

    @Override
    public void onBindViewHolder(BatallaViewHolder viewHolder, int position) {
        // TODO Auto-generated method stub
        viewHolder.txtUsuario1.setText(listBatalla.get(position).getUsuario1());
        viewHolder.txtUsuario2.setText(listBatalla.get(position).getUsuario2());
    }

    @Override
    public BatallaViewHolder onCreateViewHolder(ViewGroup parent, int arg1) {
        // TODO Auto-generated method stub
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.molde_lista, parent, false);
        BatallaViewHolder holder = new BatallaViewHolder(v); 
        return holder;
    }

    public static class BatallaViewHolder extends RecyclerView.ViewHolder{

        TextView txtUsuario1;
        TextView txtUsuario2;
        public BatallaViewHolder(View itemView) {
            super(itemView);
            // TODO Auto-generated constructor stub
            txtUsuario1 = (TextView)itemView.findViewById(R.id.txtUsuario1);
            txtUsuario2 = (TextView)itemView.findViewById(R.id.txtUsuario2);

        }

    }
}
    
asked by Kike Rodriguez 08.04.2017 в 21:26
source

1 answer

1

In this case the problem is that the class BatallasRecyclerAdapter is not instantiated correctly, when calling the method setData() marks error since the instance has null value.

This is an example, but you need to add the class because it probably receives values for initialization.

BatallasRecyclerAdapter RecycleAdapter = new BatallasRecyclerAdapter();

The correct code to initialize would be:

@Override
protected void onPostExecute(Object result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);

    Log.d(msg, "probando");

    if(batallaList != null){
        Log.d(msg, "probando1");
        adapterRecycler = new BatallasRecyclerAdapter(batallaList);
        //adapterRecycler.setData(batallaList);
        adapterRecycler.notifyDataSetChanged();
    }
}
    
answered by 08.04.2017 / 21:33
source