尝试在movie内设置DetailsLandscapeFragment.java变量的值时,我遇到一个奇怪的问题。
标记为"tag1"的日志消息正确显示了电影的标题,这意味着变量不是null,但是标记为"tag2"的日志消息将不会执行,这意味着变量为null
各自的代码是:

DetailsLandscapeFragment.java

package io.github.the_dagger.movies;


public class DetailsLandscapeFragment extends Fragment {
SingleMovie movie ;                   //Trying to set value of this Variable
TextView title;
TextView overviewTextView;
TextView releaseTextView;
ImageView posterImage;
RatingBar rb;

public void getMovie(SingleMovie singleMovie){
    this.movie = singleMovie;
    Log.v("tag1",movie.movieTitle);        //Shows the movie Title Correctly
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.details_land_frag,container,false);
    title = (TextView) view.findViewById(R.id.movieDetailTitle1);
    overviewTextView = (TextView) view.findViewById(R.id.movieSummary1);
    releaseTextView = (TextView) view.findViewById(R.id.releaseDate1);
    posterImage = (ImageView) view.findViewById(R.id.posterImageDetail1);
    rb = (RatingBar) view.findViewById(R.id.ratingBar11);
    if(movie != null) {
        Log.v("tag2",movie.movieTitle);                     //Won't run
        title.setText(movie.movieTitle);
        Picasso.with(getActivity()).load(movie.movieImage).error(R.drawable.placeholder).into(posterImage, PicassoPalette.with(movie.movieImage, posterImage).use(BitmapPalette.Profile.MUTED)
        );
        String overView = movie.movieOverView;
        String summary = "";
        float d = Float.parseFloat(movie.movieRating);
        rb.setRating((Math.round(d)/2));
        releaseTextView.setText(movie.movieReleaseDate);
        for (String sum:overView.split("(?<=[.])\\s+"))
            if(summary == "")
                summary = sum;
            else
                summary = summary + "\n" + sum;
        overviewTextView.setText(summary);
    }
    else{
        title.setText("Movie Title");
//               Picasso.with(getActivity()).load(movie.movieImage).error(R.drawable.placeholder).into(posterImage, PicassoPalette.with(movie.movieImage, posterImage).use(BitmapPalette.Profile.MUTED)
//            );
        rb.setRating(0);
        releaseTextView.setText("N/A");
        overviewTextView.setText("Summary");
    }
    return view;

}
}


MainActivity.java

package io.github.the_dagger.movies;
public class MainActivity extends AppCompatActivity implements Communicator{
DetailsLandscapeFragment f;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    f = (DetailsLandscapeFragment) getFragmentManager().findFragmentById(R.id.fragment2);
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

@Override
public void respond(SingleMovie movie) {
    if(f != null && f.isVisible()){
        f.getMovie(movie);
    }
    else{
        Intent switchIntent = new Intent(this, DetailsActivity.class)
                    .putExtra(getString(R.string.Poster), movie);
        startActivity(switchIntent);
    }
}
}


SingleMovie.java

public class SingleMovie implements Parcelable {
String movieImage;
String movieTitle;
String movieOverView;
String movieRating;
String movieReleaseDate;
String movieBackDropImage;

public SingleMovie(String image, String title, String overView, String rating, String releaseDate, String backDropImage) {
    this.movieImage = image;
    this.movieTitle = title;
    this.movieOverView = overView;
    this.movieRating = rating;
    this.movieReleaseDate = releaseDate;
    this.movieBackDropImage = backDropImage;
}

public SingleMovie(Parcel in) {
    this.movieImage = in.readString();
    this.movieTitle = in.readString();
    this.movieOverView = in.readString();
    this.movieRating = in.readString();
    this.movieReleaseDate = in.readString();
    this.movieBackDropImage = in.readString();
}

public static final Creator<SingleMovie> CREATOR = new Creator<SingleMovie>() {
    @Override
    public SingleMovie createFromParcel(Parcel in) {
        return new SingleMovie(in);
    }

    @Override
    public SingleMovie[] newArray(int size) {
        return new SingleMovie[size];
    }
};

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(movieImage);
    dest.writeString(movieTitle);
    dest.writeString(movieOverView);
    dest.writeString(movieRating);
    dest.writeString(movieReleaseDate);
    dest.writeString(movieBackDropImage);
}
}


任何帮助或线索,将不胜感激。
谢谢!

最佳答案

我猜是因为您的onCreateView在调用respond函数之前被调用。因此,必须首先在日志中获取tag 2,然后在触发respond时,将调用getMovie函数,该函数将触发tag 1日志消息。

而不是在OnCreate中进行所有设置,仅当在getMovie函数中设置了值时才应该设置值。例如您的代码可以重写为

    public void getMovie(SingleMovie singleMovie){
        this.movie = singleMovie;
        Log.v("tag1",movie.movieTitle);        //Shows the movie Title Correctly
        initMovie(movie);
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.details_land_frag,container,false);
        title = (TextView) view.findViewById(R.id.movieDetailTitle1);
        overviewTextView = (TextView) view.findViewById(R.id.movieSummary1);
        releaseTextView = (TextView) view.findViewById(R.id.releaseDate1);
        posterImage = (ImageView) view.findViewById(R.id.posterImageDetail1);
        rb = (RatingBar) view.findViewById(R.id.ratingBar11);

            title.setText("Movie Title");
    //               Picasso.with(getActivity()).load(movie.movieImage).error(R.drawable.placeholder).into(posterImage, PicassoPalette.with(movie.movieImage, posterImage).use(BitmapPalette.Profile.MUTED)
    //            );
            rb.setRating(0);
            releaseTextView.setText("N/A");
            overviewTextView.setText("Summary");

        return view;

    }
public void initMovie(SingleMovie movie)
{
if(movie != null) {
        Log.v("tag2",movie.movieTitle);                     //Won't run
        title.setText(movie.movieTitle);
        Picasso.with(getActivity()).load(movie.movieImage).error(R.drawable.placeholder).into(posterImage, PicassoPalette.with(movie.movieImage, posterImage).use(BitmapPalette.Profile.MUTED)
        );
        String overView = movie.movieOverView;
        String summary = "";
        float d = Float.parseFloat(movie.movieRating);
        rb.setRating((Math.round(d)/2));
        releaseTextView.setText(movie.movieReleaseDate);
        for (String sum:overView.split("(?<=[.])\\s+"))
            if(summary == "")
                summary = sum;
            else
                summary = summary + "\n" + sum;
        overviewTextView.setText(summary);
    }
    else{
        title.setText("Movie Title");
//               Picasso.with(getActivity()).load(movie.movieImage).error(R.drawable.placeholder).into(posterImage, PicassoPalette.with(movie.movieImage, posterImage).use(BitmapPalette.Profile.MUTED)
//            );
        rb.setRating(0);
        releaseTextView.setText("N/A");
        overviewTextView.setText("Summary");
    }
}

关于java - 在Fragment中为对象分配值时发生,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35022529/

10-13 03:42