未连接适配器;跳过布局:RecyclerView [英] No adapter attached; skipping layout : RecyclerView

查看:99
本文介绍了未连接适配器;跳过布局:RecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MainActivity.class

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "ERROR";

    private final static String API_KEY = "xxxxxxxxxxxxxx";

    int totalpage;
    int page;

    int firstVisibleItem, visibleItemCount, totalItemCount;

    ProgressBar progressBar;

    RecyclerView recyclerView;
    MoviesAdapter moviesAdapter;

    private boolean makeCall = false;
    boolean onLoding = false;


    List<Movie> movies = new ArrayList<Movie>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        recyclerView = (RecyclerView) findViewById(R.id.movies_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));

        loadData();
        moviesAdapter = new MoviesAdapter(movies, R.layout.list_item_movie, MainActivity.this);
        moviesAdapter.notifyDataSetChanged();
        recyclerView.setAdapter(moviesAdapter);

    }


    public void loadData() {

        ApiInterface apiService =
                ApiClient.getClient().create(ApiInterface.class);

        Call<String> call = apiService.getTopRatedMovies(API_KEY);


        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {

                Log.i("RetrofitOnly", response.body());


                String responceString = response.body();
                JSONObject main;

                try {
                    main = new JSONObject(responceString);
                    page = main.getInt("page");
                    totalpage = main.getInt("total_pages");
                    Log.d("PageNo==>>", page + "");
                    JSONArray jsonArray = main.getJSONArray("results");

                    try {
                        Log.d("Array==>>", jsonArray.toString() + "");


                        movies = LoganSquare.parseList(jsonArray.toString(), Movie.class);

                        if (movies == null) {
                            Toast.makeText(getApplicationContext(), "NULL", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(getApplicationContext(), "NULL NOT", Toast.LENGTH_LONG).show();
                        }


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

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

            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });

    }

}

错误

05-13 02:25:15.145 23598-23598/com.example.dhaval.retrofitonly E/RecyclerView:未连接适配器;跳过布局

05-13 02:25:15.481 23598-23598/com.example.dhaval.retrofitonly E/RecyclerView:未连接适配器;跳过布局

提前谢谢!

解决方案

在Main/UI线程中擦除方法loadData(),因为它在改造2中是ASYNCHRONOUS,因此您无需在主线程中进行操作.

参考改装-> https://futurestud.io/blog/retrofit-同步和异步请求

将结果填写到列表中后,

notifyDataSetChanged()方法onResponse()中的适配器.

movies.addAll(response.body().getResults()); moviesAdapter.notifyDataSetChanged();

MainActivity.class

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "ERROR";

    private final static String API_KEY = "xxxxxxxxxxxxxx";

    int totalpage;
    int page;

    int firstVisibleItem, visibleItemCount, totalItemCount;

    ProgressBar progressBar;

    RecyclerView recyclerView;
    MoviesAdapter moviesAdapter;

    private boolean makeCall = false;
    boolean onLoding = false;


    List<Movie> movies = new ArrayList<Movie>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        recyclerView = (RecyclerView) findViewById(R.id.movies_recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));

        loadData();
        moviesAdapter = new MoviesAdapter(movies, R.layout.list_item_movie, MainActivity.this);
        moviesAdapter.notifyDataSetChanged();
        recyclerView.setAdapter(moviesAdapter);

    }


    public void loadData() {

        ApiInterface apiService =
                ApiClient.getClient().create(ApiInterface.class);

        Call<String> call = apiService.getTopRatedMovies(API_KEY);


        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {

                Log.i("RetrofitOnly", response.body());


                String responceString = response.body();
                JSONObject main;

                try {
                    main = new JSONObject(responceString);
                    page = main.getInt("page");
                    totalpage = main.getInt("total_pages");
                    Log.d("PageNo==>>", page + "");
                    JSONArray jsonArray = main.getJSONArray("results");

                    try {
                        Log.d("Array==>>", jsonArray.toString() + "");


                        movies = LoganSquare.parseList(jsonArray.toString(), Movie.class);

                        if (movies == null) {
                            Toast.makeText(getApplicationContext(), "NULL", Toast.LENGTH_LONG).show();
                        } else {
                            Toast.makeText(getApplicationContext(), "NULL NOT", Toast.LENGTH_LONG).show();
                        }


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

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

            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {

            }
        });

    }

}

Error

05-13 02:25:15.145 23598-23598/com.example.dhaval.retrofitonly E/RecyclerView: No adapter attached; skipping layout

05-13 02:25:15.481 23598-23598/com.example.dhaval.retrofitonly E/RecyclerView: No adapter attached; skipping layout

Thanks in advance!!!

解决方案

Erase method loadData() in Main/UI Thread because it is ASYNCHRONOUS in retrofit 2 so you don't need do it in main thread.

reference retrofit -> https://futurestud.io/blog/retrofit-synchronous-and-asynchronous-requests

and notifyDataSetChanged() the adapter in method onResponse() after you fill your result to your list.

movies.addAll(response.body().getResults()); moviesAdapter.notifyDataSetChanged();

这篇关于未连接适配器;跳过布局:RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆