0
votes

This is my database structure:enter image description here

I want to get datas from a firebase database and save it inside an arraylist object to show the atributes of the object in a Textview on Android.

Ive tried this:

public class NewsViewHolder extends RecyclerView.ViewHolder {
    private View mView;
    private Context context;
    TextView titularDetalle;
    public NewsViewHolder(final View itemView) {
        super(itemView);

        titularDetalle = (TextView) itemView.findViewById(R.id.txtCabeceraDetalle);

        mView = itemView;
        context = itemView.getContext();
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DatabaseReference myRef;
                myRef = FirebaseDatabase.getInstance().getReference("noticias");

                myRef.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        for (DataSnapshot noticia : dataSnapshot.getChildren()){
                            clsNoticias noticias = noticia.getValue(clsNoticias.class);
                            ArrayList<clsNoticias> arrayNoticias = new ArrayList<clsNoticias>();
                            arrayNoticias.add(noticias);
                            titularDetalle.setText(noticias.getTitular());
                        }
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

                Intent intent = new Intent(context, ActivityDetalle.class);
                //intent.putExtra("titular",titularDetalle);
                view.getContext().startActivity(intent);


            }
        });

    }

First of all, I get a reference of all datas inside the database and im trying to get it, but I dont know well how to make this.

Moreover, when Ive done this it throws this error:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference at es.laramira.atellez.moroninfo.Models.NewsViewHolder$1$1.onDataChange(NewsViewHolder.java:48) at com.google.firebase.database.Query$1.onDataChange(Unknown Source) at com.google.android.gms.internal.zzbpx.zza(Unknown Source) at com.google.android.gms.internal.zzbqx.zzZT(Unknown Source) at com.google.android.gms.internal.zzbra$1.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

This action is inside onClick on a item.

Someone knows how to fixed it?

3
share your database structure and simply make sure titularDetalle is not null - Pavneet_Singh

3 Answers

0
votes

You have to initialize your TextView first:

titularDetalle = (TextView) findViewById (R.id.YOUR_TEXT_VIEW_ID);
titularDetalle.setText(...);

That is the ID you defined in your layout XML for that TextView.

0
votes

Your titularDetalle variable is null! You need to define an allocation space for it.

One of the ways to do that is calling

titularDetalle = (TextView) findViewById (R.id.TEXT_VIEW_ID);

before trying to call titularDetalle.setText(noticias.getTitular());

0
votes

The issue is not related to Firebase in this case. The error is stating titularDetalle is null, so it can't call .setText() on it.

Look to your layout XML for the RecyclerView item and make sure the android:id is the same name as the id you specify when initializing titularDetalle with the findViewById() method.