未连接适配器,跳过布局错误 [英] No adapter attached , skipping layout error

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

问题描述

我在互联网上找到了一些代码,我正在尝试对其进行测试,但我得到了这个没有连接适配器,跳过布局错误.我徒劳地寻找解决方案.

Hi i've found some code on the internet and i'm trying to test it but i get this No adapter attached , skipping layout Error .I've searched for solutions in vain .

这是我的 MainActivity 类:

Here is my MainActivity class :

private ArrayList<Employee> employeeList;
private ProgressDialog pDialog;
private RecyclerView recyclerView;
private EmployeesAdapter eAdapter;

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

    pDialog = new ProgressDialog(MainActivity.this);
    pDialog.setMessage("Loading Data.. Please wait...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(false);
    pDialog.show();

    //Creating an object of our api interface
    ApiService api = RetroClient.getApiService();

    /**
     * Calling JSON
     */
    Call<EmployeeList> call = api.getMyJSON();

    /**
     * Enqueue Callback will be call when get response...
     */
    call.enqueue(new Callback<EmployeeList>() {
        @Override
        public void onResponse(Call<EmployeeList> call, Response<EmployeeList> response) {
            //Dismiss Dialog
            pDialog.dismiss();

            if (response.isSuccessful()) {
                /**
                 * Got Successfully
                 */
                employeeList = response.body().getEmployee();
                recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
                eAdapter = new EmployeesAdapter(employeeList);
                RecyclerView.LayoutManager eLayoutManager = new LinearLayoutManager(getApplicationContext());
                recyclerView.setLayoutManager(eLayoutManager);
                recyclerView.setItemAnimator(new DefaultItemAnimator());
                recyclerView.setAdapter(eAdapter);
            }
        }

        @Override
        public void onFailure(Call<EmployeeList> call, Throwable t) {
            pDialog.dismiss();
        }
    });
}

我收到此错误:E/RecyclerView:未连接适配器;跳过布局有什么建议吗?

I'm getting this error : E/RecyclerView: No adapter attached; skipping layout Any recommendations ?

推荐答案

错误信息已清除

您在创建 Activity 时没有为您的 recyclerView 设置适配器.你可能认为你在onCreate方法中为recyclerView设置了适配器,但是有两个线程在运行,一个是主线程,另一个线程是你为你的recylerView设置适配器的线程.这意味着当 recylerView 需要连接适配器时,您为 recylerView 设置适配器的线程可能会阻止从网络获取数据.

you did't set adapter for your recyclerView when Activity is created. May you think that you have set adapter for recyclerView in the onCreate method , But there are two thread running, one is main thread ,another thread is the thread you set adapter for your recylerView. Which means the thread you set adapter for recylerView may block to get datas from network when recylerView need adapter be attached .

您可以通过以下代码解决此问题!

you can fix this problems as following codes !

    private final ArrayList<Employee> employeeList = new ArrayList<>();
    private ProgressDialog pDialog;
    private RecyclerView recyclerView;
    private EmployeesAdapter eAdapter;

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


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        eAdapter = new EmployeesAdapter(employeeList);
        RecyclerView.LayoutManager eLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(eLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(eAdapter);

        pDialog = new ProgressDialog(MainActivity.this);
        pDialog.setMessage("Loading Data.. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();

        //Creating an object of our api interface
        ApiService api = RetroClient.getApiService();

        /**
         * Calling JSON
         */
        Call<EmployeeList> call = api.getMyJSON();

        /**
         * Enqueue Callback will be call when get response...
         */
        call.enqueue(new Callback<EmployeeList>() {
            @Override
            public void onResponse(Call<EmployeeList> call, Response<EmployeeList> response) {
                //Dismiss Dialog
                pDialog.dismiss();

                if (response.isSuccessful()) {
                    /**
                     * Got Successfully
                     */

                    employeeList.clear();
                    employeeList.addAll(response.body().getEmployee());
                    eAdapter.notifyDataSetChanged();

                }
            }

            @Override
            public void onFailure(Call<EmployeeList> call, Throwable t) {
                pDialog.dismiss();
            }
        });
    }

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

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