Skip to content Skip to sidebar Skip to footer

No Adapter Attached , Skipping Layout Error

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 . Here is my MainA

Solution 1:

Error is because of initializing recycerlview and adapter from a "delayed" method. so to solve this do as below:

First init your arraylist as below:

privateArrayList<Employee> employeeList = new ArrayList<>();

Initialize your recyclerview before api calling, means in main thread:

recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
eAdapter = newEmployeesAdapter(employeeList);
RecyclerView.LayoutManagereLayoutManager=newLinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(eLayoutManager);
recyclerView.setItemAnimator(newDefaultItemAnimator());
recyclerView.setAdapter(eAdapter);

Then in your api response you should only update adapter if list found:

if(response!=null&&response.body()!=null&&response.body().getEmployee()!=null){employeeList.clear();employeeList.addAll(response.body().getEmployee());if(eAdapter!=null){eAdapter.notifyDataSetChanged();}}

Solution 2:

The error msg is clear .

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 !

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

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


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


        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        eAdapter = newEmployeesAdapter(employeeList);
        RecyclerView.LayoutManagereLayoutManager=newLinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(eLayoutManager);
        recyclerView.setItemAnimator(newDefaultItemAnimator());
        recyclerView.setAdapter(eAdapter);

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

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

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

        /**
         * Enqueue Callback will be call when get response...
         */
        call.enqueue(newCallback<EmployeeList>() {
            @OverridepublicvoidonResponse(Call<EmployeeList> call, Response<EmployeeList> response) {
                //Dismiss Dialog
                pDialog.dismiss();

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

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

                }
            }

            @OverridepublicvoidonFailure(Call<EmployeeList> call, Throwable t) {
                pDialog.dismiss();
            }
        });
    }

Solution 3:

Always try to initialize objects, required classes, adapters like LinearLayoutManager first before calling background task. maybe that is creating a problem in your case.

I have made some changes in your code:

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

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   employeeList = newArrayList<>;
   recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
   eAdapter = newEmployeesAdapter(employeeList);
   RecyclerView.LayoutManagereLayoutManager=newLinearLayoutManager(getApplicationContext());
   recyclerView.setLayoutManager(eLayoutManager);
   recyclerView.setItemAnimator(newDefaultItemAnimator());
   recyclerView.setAdapter(eAdapter);

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

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

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

/**
 * Enqueue Callback will be call when get response...
 */
call.enqueue(newCallback<EmployeeList>() {
    @OverridepublicvoidonResponse(Call<EmployeeList> call, Response<EmployeeList> response) {
        //Dismiss Dialog
        pDialog.dismiss();

        if (response.isSuccessful()) {
            /**
             * Got Successfully
             */if(!employeeList.isEmpty())employeeList.clear();
            employeeList.addAll(response.body().getEmployee());
            eAdapter.notifyDataSetChanged();
        }
    }

    @OverridepublicvoidonFailure(Call<EmployeeList> call, Throwable t) {
        pDialog.dismiss();
    }
});
}

Post a Comment for "No Adapter Attached , Skipping Layout Error"