Error Can not convert object of type

-2

This started to appear out of nowhere, apparently I did not change anything in the code and simply by making changes to other parts it appeared.

Error:

12-07 07:47:43.652 8947-8947/br.com.tecmafandroid.projeto.tecmaf E/AndroidRuntime: FATAL EXCEPTION: main
    Process: br.com.tecmafandroid.projeto.tecmaf, PID: 8947
    com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type br.com.tecmafandroid.projeto.tecmaf.model.Feed
        at com.google.android.gms.internal.zzelw.zzb(Unknown Source:209)
        at com.google.android.gms.internal.zzelw.zza(Unknown Source:0)
        at com.google.firebase.database.DataSnapshot.getValue(Unknown Source:10)
        at br.com.tecmafandroid.projeto.tecmaf.activity.MainActivity$8.onDataChange(MainActivity.java:272)
        at com.google.android.gms.internal.zzegf.zza(Unknown Source:13)
        at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source:2)
        at com.google.android.gms.internal.zzeig.run(Unknown Source:71)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

MainActivity:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private FirebaseAuth usuarioAutenticacao;
    private Toolbar toolbar;
    private String idUsuarioLogado;
    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle actionBarDrawerToggle;
    private FloatingActionButton floatingActionButton;
    private RecyclerView recyclerFeed;
    private FeedAdapter feedAdapter;
    private List<Feed> listaFeed = new ArrayList<>();
    private ValueEventListener valueEventListenerFeed;
    private DatabaseReference feedRef;
    private FirebaseUser firebaseUser;
    private LinearLayout linearMessages;
    private LinearLayout linearItensSalvos;

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

        idUsuarioLogado = Base64Custom.codificarBase64(UsuarioFirebase.getIdentificadorUsuario());

        linearMessages = findViewById(R.id.linearMessages);
        linearItensSalvos = findViewById(R.id.linearItensSalvos);

        linearMessages.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, MensagensActivity.class);
                startActivity(i);
            }
        });

        linearItensSalvos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ItensSalvosActivity.class);
                startActivity(intent);

            }
        });


        feedRef = ConfiguracaoFirebase.getFirebase().child("feed").child(idUsuarioLogado);

        recyclerFeed = findViewById(R.id.recyclerFeed);

        feedAdapter = new FeedAdapter(listaFeed, MainActivity.this);
        recyclerFeed.setHasFixedSize(true);
        recyclerFeed.setLayoutManager(new LinearLayoutManager(MainActivity.this));
        recyclerFeed.setAdapter(feedAdapter);

        usuarioAutenticacao = ConfiguracaoFirebase.getFirebaseAutenticacao();

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("Tecmaf");

        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        floatingActionButton = findViewById(R.id.floatingActionButtonPublicacao);

        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                abrirPublicacao();
            }
        });

    }

    @Override
    protected void onStop() {
        super.onStop();

        feedRef.removeEventListener(valueEventListenerFeed);
    }

    private void listarFeed() {

        valueEventListenerFeed = feedRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                listaFeed.clear();

                for (DataSnapshot ds : dataSnapshot.getChildren()) {

                    listaFeed.add(ds.getValue(Feed.class));

                }

                Collections.reverse(listaFeed);
                feedAdapter.notifyDataSetChanged();

            }


            @Override
            public void onCancelled(DatabaseError databaseError) {

            }


        });


    }

}

Model:

    public class Feed {

    private String id;
    private String fotoPostagem;
    private String descricao;
    private String nomeUsuario;
    private String fotoUsuario;

    public Feed() {
    }

    public String getFotoPostagem() {
        return fotoPostagem;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setFotoPostagem(String fotoPostagem) {
        this.fotoPostagem = fotoPostagem;
    }

    public String getDescricao() {
        return descricao;
    }

    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }

    public String getNomeUsuario() {
        return nomeUsuario;
    }

    public void setNomeUsuario(String nomeUsuario) {
        this.nomeUsuario = nomeUsuario;
    }

    public String getFotoUsuario() {
        return fotoUsuario;
    }

    public void setFotoUsuario(String fotoUsuario) {
        this.fotoUsuario = fotoUsuario;
    }
}

Firebase Structure:

    
asked by anonymous 07.12.2018 / 11:06

1 answer

0

I believe the problem in your code is in the listarFeed() method in the following snippet:

for (DataSnapshot ds : dataSnapshot.getChildren()) {

    listaFeed.add(ds.getValue(Feed.class));
}

Test without Loop to identify if DataSnapshot is bursting error.

Try something like this:

listaFeed.clear();
Feed feed = ds.getValue(Feed.class);
listaFeed.Add(feed);

I hope I have helped in some way.

    
07.12.2018 / 11:31