Oi, pessoal estou aprendendo a programar para android.
Estou criando um app bem básico para aprender, mas na hora de rodar o emulador
aparece a mensagem “Unfortunately, Filmes Populares has stopped.”
Sinto que estou cometendo um erro muito amador .
Segue abaixo o código da Activity, Adapter e os erros indicados pelo Android Studio ListaFilmesActivity.java
package com.example.filmespopulares.filme.ui.listafilmes;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.widget.Toast;import com.example.filmespopulares.R;
import java.util.Arrays;
import java.util.List;import data.model.data.network.ApiService;
import data.model.Filme;
import data.model.data.network.response.FilmesResult;
import data.model.mapper.FilmeMapper;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;public class ListaFilmesActivity extends AppCompatActivity {
private RecyclerView recyclerFilmes; private ListaFilmesAdapter filmesAdapter; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_lista_filmes); configuraToolbar(); configuraAdapter(); obterFilmes(); } private void configuraToolbar(){ Toolbar toolbar = findViewById(R.id.my_toolbar); setSupportActionBar(toolbar); } private void configuraAdapter(){ recyclerFilmes = findViewById(R.id.recycler_filmes); filmesAdapter = new ListaFilmesAdapter(); RecyclerView.LayoutManager gridLayoutManager = new GridLayoutManager(this, 2); recyclerFilmes.setLayoutManager(gridLayoutManager); recyclerFilmes.setAdapter(filmesAdapter); } private void obterFilmes(){ ApiService.getInstance() .obterFilmesPopulares("990ae2bd58e6d10f42842f81599fcf5c") .enqueue(new Callback<FilmesResult>() { @Override public void onResponse(Call<FilmesResult> call, Response<FilmesResult> response) { if (response.isSuccessful()){ final List<Filme> listaFilmes = FilmeMapper .deResponseParaDominio(response.body().getResultadoFilmes()); filmesAdapter.setFilmes(listaFilmes); }else { mostraErro(); } } @Override public void onFailure(Call<FilmesResult> call, Throwable t) { mostraErro(); } }); } private void mostraErro(){ Toast.makeText(this, "Erro ao obter a lista de filmes", Toast.LENGTH_SHORT).show(); }
}
ListaFilmesAdapter.java
public class ListaFilmesAdapter extends RecyclerView.Adapter<ListaFilmesAdapter.ListaFilmesViewHolder> {
private List<Filme> Filmes; public ListaFilmesAdapter(){ Filmes = new ArrayList<>(); } @NonNull @Override public ListaFilmesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_filme, parent, false); return new ListaFilmesViewHolder(view); } @Override public void onBindViewHolder(@NonNull ListaFilmesViewHolder holder, int position) { holder.bind(Filmes.get(position)); } @Override public int getItemCount() {return (Filmes != null && Filmes.size() > 0) ? Filmes.size() : 0; } static class ListaFilmesViewHolder extends RecyclerView.ViewHolder{ private TextView textTituloFilme; private ImageView imagePosterFilme; public ListaFilmesViewHolder(View itemView) { super(itemView); textTituloFilme = itemView.findViewById(R.id.text_titulo_filme); imagePosterFilme = itemView.findViewById(R.id.image_poster_filme); } public void bind(Filme filme){ textTituloFilme.setText(filme.getTitulo()); Picasso.get() .load("https://image.tmdb.org/t/p/w342/" + filme.getCaminhoPoster()) .into(imagePosterFilme); } } public void setFilmes(List<Filme> filmes){ this.Filmes = filmes; notifyDataSetChanged(); }
}
Erros apontados pelo Android Studio
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.filmespopulares, PID: 6867
java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.ImageView
at com.example.filmespopulares.filme.ui.listafilmes.ListaFilmesAdapter$ListaFilmesViewHolder.(ListaFilmesAdapter.java:52)
at com.example.filmespopulares.filme.ui.listafilmes.ListaFilmesAdapter.onCreateViewHolder(ListaFilmesAdapter.java:31)
at com.example.filmespopulares.filme.ui.listafilmes.ListaFilmesAdapter.onCreateViewHolder(ListaFilmesAdapter.java:18)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6794)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5975)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:557)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:171)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4194)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2086)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1843)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Loo
I/Process: Sending signal. PID: 6867 SIG: 9
Process 6867 terminated.