【问题标题】:No adapter attached; skipping layout : RecyclerView [duplicate]未连接适配器;跳过布局:RecyclerView [重复]
【发布时间】:2016-09-09 05:18:00
【问题描述】:

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:没有附加适配器;跳过布局

提前致谢!!!

【问题讨论】:

    标签: android android-recyclerview retrofit


    【解决方案1】:

    在 Main/UI 线程中擦除方法 loadData(),因为它是改造 2 中的 ASYNCHRONOUS,因此您不需要在主线程中执行此操作。

    参考改造 -> https://futurestud.io/blog/retrofit-synchronous-and-asynchronous-requests

    notifyDataSetChanged() 方法onResponse() 中的适配器,在您将结果填充到列表后。

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

    【讨论】:

      猜你喜欢
      • 2021-01-11
      • 2021-05-04
      • 1970-01-01
      • 1970-01-01
      • 2021-04-24
      • 2020-03-12
      • 1970-01-01
      • 1970-01-01
      • 2015-04-15
      相关资源
      最近更新 更多