RecyclerView - 没有连接适配器;跳过布局

时间:2018-02-01 20:38:44

标签: android android-recyclerview adapter

我知道这个问题已经发布但是我尝试了所有解决方案并且错误仍然存​​在。当我调用片段从json加载一些数据并在recyclerview中设置时,调试应用我收到此错误

  

RecyclerView:没有连接适配器;跳过布局

Fragement

public class Artists extends  Fragment {
    String URL_DATA = JsonData.FirebaseList;
    RecyclerView.LayoutManager layoutManager;
    ProgressBar pb;
    private List<Post_Artist> post_artists;
    private RecyclerView recyclerView;
    private TextView loading;
    private AdapterFor_Artists adapter;
    private AdView mAdView;

    public Artists() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_artists, container, false);
        post_artists = new ArrayList<>();
        recyclerView = v.findViewById(R.id.rc);
        layoutManager = new GridLayoutManager(getActivity(),1);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        loading = v.findViewById(R.id.loadingtext);
        pb = v.findViewById(R.id.loadingP);
        Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(),"ARCADECLASSIC.TTF");
        loading.setTypeface(typeface);
        if (adapter == null) {
        adapter = new AdapterFor_Artists(post_artists, getActivity(), new RecyclerViewClickListenner_ForArtist() {
            @Override
            public void onClick(View view, Post_Artist post_artist) {
                Intent intent = new Intent(getActivity(), ArtistsPage.class);
                intent.putExtra("json_url", post_artist.getJsonUrl());
                intent.putExtra("name", post_artist.getArtistName());
                startActivity(intent);
                getActivity().overridePendingTransition(R.anim.fadein, R.anim.fadeout);
            }
        });
        recyclerView.setAdapter(adapter);
        checkInternet();
        return v;
    }

    private boolean checkInternet() {
        if (!isNetworkConnected()) {
            showDialog();

            return false;
        } else {
            refreshContent();
        }
        return true;
    }



    public boolean isNetworkConnected() {
        ConnectivityManager connectionManager = (ConnectivityManager) getActivity().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectionManager == null) {
            return false;
        }
        NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();
        return networkInfo != null && networkInfo.isConnected();
    }

    private void refreshContent() {
        fetchRemoteData(new DataStatus_For_ArtistsList() {
            @Override
            public void onSuccess(List<Post_Artist> post_artists) {
                loading.setVisibility(View.GONE);
                pb.setVisibility(View.GONE);
                    adapter.getItems().clear();
                    adapter.getItems().addAll(post_artists);
                    adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Exception e) {
                msgError();
            }
        });
    }


    }
    private void fetchRemoteData(final DataStatus_For_ArtistsList callback) {
        StringRequest stringRequest = new StringRequest(Request.Method.GET,
                URL_DATA,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String s) {
                        List<Post_Artist> listItems = new ArrayList<>();
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            JSONArray array = jsonObject.getJSONArray("bgs");
                            for (int i = 0; i < array.length(); i++) {
                                JSONObject o = array.getJSONObject(i);
                                Post_Artist item = new Post_Artist(
                                        o.optString("json_url"),
                                        o.optString("name"),
                                        o.optString("cover")

                                );
                                listItems.add(item);
                            }
                            callback.onSuccess(listItems);
                        } catch (JSONException e) {
                        }

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

                    }
                });
        RequestQueue requestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());
        requestQueue.add(stringRequest);
    }

1 个答案:

答案 0 :(得分:0)

您必须在onCreateView()中使用post_artists列表设置适配器。您的适配器为null,这就是您遇到此错误的原因。