gpt4 book ai didi

java - 为 Fragment 中的对象赋值时出现问题

转载 作者:行者123 更新时间:2023-12-01 21:52:14 25 4
gpt4 key购买 nike

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

详细信息LandscapeFragment.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 日志消息。

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

    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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com