Can’t Convert error Object of type

Asked

Viewed 188 times

-2

This one started to appear out of nowhere, apparently I didn’t change anything in the code and simply by making changes in 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 Estrutura:

inserir a descrição da imagem aqui

  • To solve it, I checked how I was saving the data in the feed node and was saving it differently than the Feed model presented, so it could not convert.

1 answer

0

I believe the problem with your code is the method listarFeed() on the stretch below:

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

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

Do a test without the Loop to identify if the DataSnapshot is screwing up.

Try something like that:

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

I hope I’ve helped in some way.

  • If I don’t use the loop, there’s no way I can give getValue because I don’t have Datasnapshot

  • The strange thing is that it was just like this before!

  • I found the error already, is has nothing to do with the code but as the data was being saved in feed node

  • Good!!! Put the resolution later if possible hehe.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.