在Fragment中为对象赋值时发出问题

时间:2016-01-26 19:45:59

标签: java android

尝试在response = ''' { "channels": [{ "channel": { "id": "1", "title": "test1", "replay": false, "description": "", "epg_id": null, "genre": "35", "pin": false, "server": "1", "genre": "Other", "logo": "https:\/\/somewebsite.com\/1.jpg", "url1": "http:\/\/somewebsite2.com:80\/live\/hls\/1.m3u8", "url2": "http:\/\/somewebsite2:80\/live\/hls\/1.m3u8", "dvr_url": "" } }, { "channel": { "id": "2", "title": "test2", "replay": false, "description": "", "epg_id": null, "genre": "27", "pin": false, "server": "2", "genre": "Movies", "logo": "https:\/\/somewebsite.com\/2.jpg", "url1": "http:\/\/somewebsite2.com:80\/live\/hls\/2.m3u8", "url2": "http:\/\/somewebsite2.com:80\/live\/hls\/2.m3u8", "dvr_url": "" } }] }''' import json data = json.loads(response) channels = data['channels'] for item in channels: name = item['channel']['title'] thumb = item['channel']['logo'] cat = item['channel']['genre'] url = item['channel']['url1'] #add_link(name, url, 4, thumb, thumb) print(name, url, thumb, cat) 内设置movie变量的值时,我遇到了一个奇怪的问题。 带有标记DetailsLandscapeFragment.java的日志消息正确显示了影片的标题,这意味着该变量不为空,但标记为"tag1"的日志消息将不会被执行,这意味着该变量为{{1} }。 各自的代码是:

DetailsLandscapeFragment.java

"tag2"

MainActivity.java

null

SingleMovie.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;

}
}

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

2 个答案:

答案 0 :(得分:2)

我想这是因为您的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");
    }
}

答案 1 :(得分:1)

原因是您的片段上的public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)public void getMovie(SingleMovie singleMovie)之前执行。要查看首先调用哪个方法,您可以将Log.v("tag2",movie.movieTitle);移到if(movie != null)语句之上(处理空电影对象!!!)并在日志中看到它“tag2”或“tag1”是先印了。

你必须找到一种方法在onCreateView内设置电影对象或从点移动代码

if(movie != null) {
    ...
    overviewTextView.setText(summary);
}

onCreateViewgetMovie