Error Unable to start activity ComponentInfo cannot be cast to com.example.mycinemaapp.models.TVShowDetails

  android, java, mvvm, rest

There is an application that displays information about movies using the REST-API based on the MVVM pattern. The application already displays movies in Recycle, but for some reason I don’t get detailed information. I’m sure that I wrote something wrong in the Api-Client class. But that I can’t understand that’s why I’m asking for your help in this difficult matter, learning programming =) It turns out that I indicated in the Movie-api interface

// Details
// https://www.episodate.com/api/show-details?q=29560
@GET("https://www.episodate.com/api/show-details")
Call<MovieDetailsResponse> movieDetails(
 @Query("q") String query
);

In order not to load you with code, I will show only the MovieApiClient class and part of the code from the MovieDetails activations. As in the Response, Repository and MovieListViewModel classes, I’m sure there is nothing complicated there. And I’ll immediately clarify that the TVShowDetails class is a model of detailed information

public class MovieApiClient {

private MutableLiveData<List<TVShowDetails>> mDetailsMovies;
private RetrieveMoviesDetails retrieveMoviesDetails;

public static MovieApiClient getInstance() {
     if(instance == null){
     instance = new MovieApiClient();
}
return instance;
}

private MovieApiClient() {
mMovies = new MutableLiveData<>();
mDetailsMovies = new MutableLiveData<>();
}

public LiveData<List<TVShowDetails>> getTvShowDetails() {
return mDetailsMovies;
}

public void movieDetails(String query){

if(retrieveMoviesDetails != null){
    retrieveMoviesDetails = null;
}

retrieveMoviesDetails = new RetrieveMoviesDetails(query);
final Future myHandler = AppExecutors.getInstance().networkIO().submit(retrieveMoviesDetails);

AppExecutors.getInstance().networkIO().schedule(new Runnable() {
    @Override
    public void run() {
        myHandler.cancel(true);
    }
}, 4000, TimeUnit.MILLISECONDS );
}

private class RetrieveMoviesDetails implements Runnable{

private String query;
boolean cancelRequest;

public RetrieveMoviesDetails(String query){
    this.query = query;
    cancelRequest = false;
}

@Override
public void run() {
    // Получаем Response объекты
    try {
        Response response = getTvShowDetails(query).execute();
        if(cancelRequest){
            return;
        }
        if (response.code() == 200){

            List<TVShowDetails> list_tv_details = new ArrayList<> 
    (((MovieDetailsResponse)response.body()).getList_tv_details());
            mDetailsMovies.postValue(list_tv_details);

        } else {
            String error_details = response.errorBody().string();
            Log.v("Tag", "Error code" +error_details);
            mDetailsMovies.postValue(null);

        }
    } catch (IOException e) {
        e.printStackTrace();
    }

   }
    private Call<MovieDetailsResponse> getTvShowDetails(String query){
    return Servicey.getMovieApi().movieDetails(query);
    }

private void cancelRequest(){
    Log.v("Tag","Cancelling Search Request");
    cancelRequest = true;
}

}

I guess I made a mistake somewhere in the RetrieveMoviesDetails in run, because that’s where I don’t know much about what’s going on. And here is the class in which I display the information I need

  private void getDataFromIntent() {
  if(getIntent().hasExtra("movie")){
    MovieModel movieModel = getIntent().getParcelableExtra("movie"); // another model in it inf.
  only for the initial screen, there is no detailed there, but to get a Poster, the name is suitable 
  and it working.
    TVShowDetails tvShowDetails = getIntent().getParcelableExtra("movie");
    description_movie.setText(tvShowDetails .getDescription());
   }

Here are the logs when the DetailsActivity starts

java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.example.mycinemaapp/com.example.mycinemaapp.MovieDetails}: 
java.lang.ClassCastException: com.example.mycinemaapp.models.MovieModel cannot be cast to 
 com.example.mycinemaapp.models.TVShowDetails
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2511)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2571)
at android.app.ActivityThread.access$900(ActivityThread.java:165)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1409)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:171)
at android.app.ActivityThread.main(ActivityThread.java:5620)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
Caused by: java.lang.ClassCastException: com.example.mycinemaapp.models.MovieModel cannot be cast to 
com.example.mycinemaapp.models.TVShowDetails
at com.example.mycinemaapp.MovieDetails.getDataFromIntent(MovieDetails.java:60)
at com.example.mycinemaapp.MovieDetails.onCreate(MovieDetails.java:33)
at android.app.Activity.performCreate(Activity.java:6320)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1121)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2464)

Actually, that’s all, I understand that a lot of code still came out … Help me figure it out please, I really want to understand what I’m doing wrong and I have no one else to ask.

Source: Android Questions

LEAVE A COMMENT