2016-04-28 8 views
1

最近、Retrofitで実験が開始されました。具体的には、themoviedb.org APIAndroid:NPE、getIntent()。DetailActivity(IDフィールドを持つ2つの異なるjsonモデルクラス)のgetParcelableExtra()が2回追加されました

現在、人気のある映画の一覧をMainActivityに表示しています。ムービーをクリックするとDetailActivityが起動し、キャストやクルーを含む特定のムービーの詳細が表示されます。キャストメンバーをクリック

FilmographyActivity起動 - 特定の俳優がでてきた各映画のための映画のポスターを表示

問題:私は、その後の映画をクリックしようとしている FilmographyActivity - >その映画のユニークなIDを取得し、DetailsActivityに渡してください。

MovieDetailActivity.java

public static final String EXTRA_MOVIE = "movie"; 
public static final String EXTRA_ACTOR = "actor"; 
MovieInfo mMovieInfo; 
ActorCast mActorCast; 

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

     // Here's where I think/know I'm screwing things up: 
     if (getIntent().hasExtra(EXTRA_MOVIE)) { 
      mMovieInfo = getIntent().getParcelableExtra(EXTRA_MOVIE); 
     } else if (getIntent().hasExtra(EXTRA_ACTOR)){ 
      mActorCast = getIntent().getParcelableExtra(EXTRA_ACTOR); 
     } else { 
      throw new IllegalArgumentException("Detail Activity IllegalArgumentException"); 
    } 

    final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    if (getSupportActionBar() != null) { 
     getSupportActionBar().setTitle(mMovieInfo.getTitle()); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    } 

    title.setText(mMovieInfo.title); 
    description.setText(mMovieInfo.overview); 
    voteCount.setText(String.valueOf(mMovieInfo.voteCount)); 

    ..... 

} // end onCreate 

関連するコード(?それとも、これらは私がhttp://www.jsonschema2pojo.org/を使用してから生成したクラスの2つですList<MovieInfo>からIDに等しいList<ActorCast>からIDを設定することが可能です)

そしてPopularMoviesAdapterの一部:

private Context mContext; 
    private List<MovieInfo> list; 
    private LayoutInflater inflater; 

    public MovieAdapter(Context context) { 
    inflater = LayoutInflater.from(context); 
    this.mContext = context; 
    list = new ArrayList<>(); 
} 

@Override 
    public void onBindViewHolder(final MyViewHolder holder, final int position) { 
     final MovieInfo currentMovie = list.get(position); 

    ... 
     holder.moviePoster.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(v.getContext(), "Clicked: " + currentMovie.getId(), Toast.LENGTH_SHORT).show(); 
      int position = holder.getAdapterPosition(); 
      Intent intent = new Intent(mContext, MovieDetailActivity.class); 
      intent.putExtra(MovieDetailActivity.EXTRA_MOVIE, list.get(position)); 
      mContext.startActivity(intent); 

     } 
    }); 
} // end onBindViewHolder 

そしてFilmographyAdapterの一部:

private Context mContext; 
private LayoutInflater inflater; 
private List<ActorCast> list; 

public ActorAdapter(Context context, List<ActorCast> actorCast){ 
    inflater = LayoutInflater.from(context); 
    this.mContext = context; 
    this.list = actorCast; 
} 

..... 

@Override 
public void onBindViewHolder(final ActorViewHolder holder, final int position) { 
    final ActorCast actor = list.get(position); 

     holder.actorCreditMovieImage.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(v.getContext(), "Clicked: " + actor.getId(), Toast.LENGTH_SHORT).show(); 
      Intent intent = new Intent(mContext, MovieDetailActivity.class); 
      //intent.putExtra(MovieDetailActivity.EXTRA_ACTOR, actorInfoList.get(position).getId()); 

      intent.putExtra(MovieDetailActivity.EXTRA_ACTOR, list.get(position).id); 
      mContext.startActivity(intent); 
     } 
    }); 

私はおそらくバックステップを取ると、何が起こっているかの「全体像」を見てする必要があります - あまりにも長いビットのためにそれを見つめられて。

public interface PopMoviesInterface { 

    @GET("3/discover/movie?sort_by=popularity.desc&api_key=308be8a6ec40318471fb3c4d177c0382") 
    Call<MovieResult> allItems(@Query("page") int page); 

    @GET("3/person/{id}/movie_credits") 
    Call<ActorResult> showActorHistory(@Path("id") String id, @Query("api_key") String key); 

    @GET("3/movie/{movie}/credits") 
    Call<CreditsList> listCredits(@Path("movie") String movie, @Query("api_key") String key); 

} 

とlogcat:

04-27 21:47:06.596 19486-19486/com.example.user.retrofit2moviedemo E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.example.user.retrofit2moviedemo, PID: 19486 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.retrofit2moviedemo/com.example.user.retrofit2moviedemo.MovieDetailActivity}: java.lang.IllegalArgumentException: Detail Activity IllegalArgumentException 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Caused by: java.lang.IllegalArgumentException: Detail Activity IllegalArgumentException 
at com.example.user.retrofit2moviedemo.MovieDetailActivity.onCreate(MovieDetailActivity.java:101) 
at android.app.Activity.performCreate(Activity.java:6251) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  
at android.app.ActivityThread.-wrap11(ActivityThread.java)  
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  
at android.os.Handler.dispatchMessage(Handler.java:102)  
at android.os.Looper.loop(Looper.java:148)  
at android.app.ActivityThread.main(ActivityThread.java:5417)  
at java.lang.reflect.Method.invoke(Native Method)  
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  

任意の提案、助言または右方向へのポイントが高く評価され

は、ここに私のインターフェイスの呼び出しがどのように見えるかです。ここで

編集は私のPOJOです。注:MovieInfoのすべての「フィールド」(私はこの用語を正しく使用していますか?)は、「overview」「voteCount」などのActorCastにあります。したがって、FilmographyActivityでムービーをクリックすると、ActorCast.javaにそのフィールドが見つからないため、概要などが自動的に取得されるだけではありません。 MovieResult.java

public class MovieInfo implements Parcelable { 

    @SerializedName("poster_path") 
    @Expose 
    public String posterPath; 
    @SerializedName("adult") 
    @Expose 
    public Boolean adult; 
    @SerializedName("overview") 
    @Expose 
    public String overview; 
    @SerializedName("release_date") 
    @Expose 
    public String releaseDate; 
    @SerializedName("id") //THIS IS THE UNIVERSAL MOVIE ID 
    @Expose 
    public Integer id; 
    @SerializedName("original_title") 
    @Expose 
    public String originalTitle; 
    ... // Not showing Other fields like popularity, voteCount etc to save space 
    ... 
    // getters and setters 

    public static final Creator<MovieInfo> CREATOR = new Creator<MovieInfo>() { 
     @Override 
     public MovieInfo createFromParcel(Parcel in) { 
      return new MovieInfo(in); 
     } 
     @Override 
     public MovieInfo[] newArray(int size) { 
      return new MovieInfo[size]; 
     } 
    }; 

    public MovieInfo(Parcel in){ 
     id = in.readInt(); 
     title = in.readString(); 
     posterPath = in.readString(); 
     overview = in.readString(); 
     backdropPath = in.readString(); 
     voteAverage = in.readDouble(); 
     voteCount = in.readInt(); 
     releaseDate = in.readString(); 
    } 

    public MovieInfo(int id, String title, String poster, String plot, 
       double rating, String release, String backdrop, int voteCount) { 
     this.id = id; 
     this.title = title; 
     this.posterPath = poster; 
     this.overview = plot; 
     this.backdropPath = backdrop; 
     this.voteAverage = rating; 
     this.voteCount = voteCount; 
     this.releaseDate = release; 
    } 

     @Override 
     public int describeContents() { 
      return 0; 
     } 

     @Override 
     public void writeToParcel(Parcel parcel, int i) { 
      parcel.writeInt(id); 
      parcel.writeString(title); 
      parcel.writeString(posterPath); 
      parcel.writeString(overview); 
      parcel.writeString(backdropPath); 
      parcel.writeDouble(voteAverage); 
      parcel.writeInt(voteCount); 
      parcel.writeString(releaseDate); 
     } 

MovieResult.java

@Generated("org.jsonschema2pojo") 
public class MovieResult { 

    @SerializedName("page") 
    @Expose 
    public Integer page; 
    @SerializedName("results") 
    @Expose 
    public List<MovieInfo> results = new ArrayList<MovieInfo>(); 
    @SerializedName("total_results") 
    @Expose 
    public Integer totalResults; 
    @SerializedName("total_pages") 
    @Expose 
    public Integer totalPages; 
    // getters and setters 

フィルモグラフィーとの契約下の2つに接続されている

MovieInfo.java

ActorCast.java

@Generated("org.jsonschema2pojo") 
public class ActorCast implements Parcelable { 

    @SerializedName("adult") 
    @Expose 
    public Boolean adult; 
    @SerializedName("character") 
    @Expose 
    public String character; 
    @SerializedName("credit_id") 
    @Expose 
    public String creditId; 
    @SerializedName("id") //THIS IS THE UNIVERSAL MOVIE ID 
    @Expose 
    public Integer id; 
    // few other fields not included to save space. 
    // getters and setters 
    // Parcelable methods here 

そしてActorResult。Java

@Generated("org.jsonschema2pojo") 
public class ActorResult { 

    @SerializedName("cast") 
    @Expose 
    public List<ActorCast> cast = new ArrayList<ActorCast>(); 
    @SerializedName("crew") 
    @Expose 
    public List<ActorCrew> crew = new ArrayList<ActorCrew>(); 
    @SerializedName("id") //THIS IS THE ACTOR ID 
    @Expose 
    public Integer id; 

答えて

0

NPEのためにMovieDetailActivityでIDを取得できないという問題はありますか?それはタイトルが示しているとおりですので、getParceableExtra(EXTRA_MOVIE);の代わりにgetSerializableExtra(EXTRA_MOVIE);があなたの後にあると思います。

+0

貧弱な言葉のタイトルにはお詫び申し上げます。 Parcelableを実装しているPOJOのいくつかを含めるように質問を編集しました。例えば、私が映画 "Deadpool"をクリックすると、私のトーストはID#293660を出し、詳細活動を開始します。 「詳細アクティビティ」にも映画のキャストが表示されているので、「Ryan Reynolds」をクリックします。最後に、これはReynoldsのFilmography Activityに連れて行きます。私が映画をスクロールして「Deadpool」をクリックすると、私のToastは正しい映画IDを表示します。しかし、これをDetailsアクティビティに渡すと、クラッシュすることになります。私はSerializableを使うべきですか? – ImArtVandalay

関連する問題