没有设置或显示片段

时间:2019-03-22 12:59:03

标签: android json android-fragments

我正在从Json获取数据,并且想在片段textview中设置值 在日志中,我获得了所有数据,但是我只看到手机中的空白片段,什么都没设置!

public class FragmentReviews extends Fragment {

    View v;
    private Movie movie;
    private String movieId;
    private LinearLayout mGallery;
    private LayoutInflater mInflater;
    private RequestQueue queue;

    public FragmentReviews() {
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.review_fragment, container, false);

        mGallery = (LinearLayout) v.findViewById(R.id.movie_reviews_gallery);
        return v;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mInflater = LayoutInflater.from(getActivity());

        queue = Volley.newRequestQueue(getActivity());

        movie = (Movie) getActivity().getIntent().getSerializableExtra("movie");
        movieId = movie.getMovieId();

        getreviews(movieId);
    }

    private void getreviews(String movieid) {

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,
                Constants.MAIN_INFO_URL_LEFT + movieid + Constants.MAIN_INFO_URL_RIGHT, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                try {
                    JSONObject review = response.getJSONObject("reviews");
                    JSONArray reviews = review.getJSONArray("results");

                    String name = null;
                    String text = null;

                    if (reviews.length() > 0) {

                        for (int i = 0; i <= reviews.length(); i++) {

                            JSONObject rev = reviews.getJSONObject(i);
                            name = rev.getString("author");
                            text = rev.getString("content");
                            View view = mInflater.inflate(R.layout.index_reviews_gallery,
                                    mGallery, false);
                            TextView rname = (TextView) view
                                    .findViewById(R.id.id_index_reviews_name);
                            rname.setText(name + ":");
                            JustifiedTextView revtext = (JustifiedTextView) view
                                    .findViewById(R.id.id_index_reviews_text);
                            revtext.setText(text);
                            mGallery.addView(view);
                        }

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


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        queue.add(jsonObjectRequest);
    }
}

我不明白问题出在哪里,为什么我无法在片段中看到视图,也感谢您的帮助。

我不知道该说些什么,但是堆栈溢出只是出现一个错误,我需要更多地飞机降落!!!!!我希望这足够了。

2 个答案:

答案 0 :(得分:0)

-可能的原因之一是您在getreviews之前的onCreate中调用onCreateView的{​​{1}},从而初始化了mGallery。您可以尝试在重设v之前和之后getreviews mGallery = (LinearLayout) v.findViewById(R.id.movie_reviews_gallery);调用ber
-也不会在onErrorResponse中打印错误,只是建议在其中放入一些错误的日志。 -在较新的android版本中,默认情况下HTTP连接被阻止。
:)

答案 1 :(得分:0)

谢谢大家。 问题是viewpager layout_height无法在wrap_content上使用!