-3

I am working on a fragment with firebase on a chat app, however in fragment I am getting this error

04-24 09:26:44.602 3554-3554/snowfox.nightshades E/RecyclerView: No adapter attached; skipping layout
04-24 09:26:44.655 3554-3554/snowfox.nightshades I/Choreographer: Skipped 32 frames!  The application may be doing too much work on its main thread.
04-24 09:26:44.825 3554-3554/snowfox.nightshades W/art: Before Android 4.1, method int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
04-24 09:26:44.915 3554-3554/snowfox.nightshades E/RecyclerView: No adapter attached; skipping layout

and nothing displays in the recyclerview. i am not able to understand why I am getting this error even after this using "chatList_rv.setAdapter(adapter);"

here is my code

public class fragment_chat extends Fragment {

    ArrayList<Chats_class> chatList;
    RecyclerView chatList_rv;
    LinearLayoutManager layoutManager;
    RecyclerView.Adapter adapter;
    DatabaseReference databaseRef, rootRef;
    SharedPreferences prefs,prefs_database;

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

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

    }

    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public View onCreateView(
            @NonNull LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view      = inflater.inflate(R.layout.fragment_chat_home, container, false);
        chatList       = new ArrayList<>();
        chatList_rv    = view.findViewById(R.id.chat_list);
        layoutManager  = new LinearLayoutManager(this.getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        adapter        = new Chat_Messages_Adapter(chatList, this.getContext());
        chatList_rv.setLayoutManager(layoutManager);
        chatList_rv.setAdapter(adapter);
        prefs          = getContext().getSharedPreferences(getString(R.string.USER_DATA), MODE_PRIVATE);
        prefs_database = getContext().getSharedPreferences(getString(R.string.USER_CHAT_DATA), MODE_PRIVATE);
        rootRef        = FirebaseDatabase.getInstance().getReference();
        databaseRef    = rootRef.child("CHAT_LISTS").child(prefs.getString("uid", "error"));
        chatList       = getList();
        databaseRef.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                String info = dataSnapshot.getValue(String.class);
                String last_c = info.substring(info.indexOf("#") + 1);
                int time;
                try {
                    time = Integer.parseInt(info.substring(0, info.indexOf("#")));
                }
                catch (Exception ignore) {
                    time = 0;
                }
                getUserInfo_andSaveItToArrayList(dataSnapshot.getKey(),last_c,time);

            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
        return view;
    }

    private ArrayList<Chats_class> getList() {
        Gson gson = new Gson();
        ArrayList<Chats_class> productFromShared;
        String jsonPreferences = prefs_database.getString(getString(R.string.USER_CHAT_HISTORY_ARRAY_LIST), "");

        Type type = new TypeToken<ArrayList<Chats_class>>() {}.getType();
        productFromShared = gson.fromJson(jsonPreferences, type);

        return productFromShared;

    }

    void getUserInfo_andSaveItToArrayList(final String uid, final String lastMsg, final int time) {
        rootRef.child("USERS").child(uid).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                UserData ud = dataSnapshot.getValue(UserData.class);
                Chats_class c = new Chats_class(ud.getIconID(),ud.getHasPic(),ud.getName(),lastMsg, uid, time);
                sync_database(c);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }
    void sync_database(Chats_class chats) {
        if(chatList != null) {
            boolean found = false;
            for (int i = 0; i < chatList.size(); i++) {
                if (chatList.get(i).getUid().equals(chats.getUid()))
                    if (chatList.get(i).equals(chats)) {
                        chatList.add(i, chats);
                        adapter.notifyItemChanged(i);
                        found = true;
                        break;
                    }
            }
            if(!found) {
                chatList.add(chats);
                adapter.notifyItemInserted(chatList.size() -1);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        SharedPreferences.Editor prefsEditor = prefs_database.edit();
        Gson gson = new Gson();
        String json = gson.toJson(chatList);
        prefsEditor.putString(getString(R.string.USER_CHAT_HISTORY_ARRAY_LIST), json);
        prefsEditor.apply();
    }
}

My layout file

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.RecyclerView android:padding="16dp"
    android:id="@+id/chat_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

</android.support.v7.widget.RecyclerView>

Can anyone help me to find what causes this error?

Snowfox
  • 69
  • 2
  • 9
  • 1
    Possible duplicate of [recyclerview No adapter attached; skipping layout](https://stackoverflow.com/questions/29141729/recyclerview-no-adapter-attached-skipping-layout) – AskNilesh Apr 24 '18 at 04:16

2 Answers2

1

If you check the source code for RecycleView here:

https://android.googlesource.com/platform/frameworks/support/+/247185b98675b09c5e98c87448dd24aef4dffc9d/v7/recyclerview/src/android/support/v7/widget/RecyclerView.java

The "error" message simply means that when the view's onLayout method is called by the parent (typically after inflation some time) it has no adapter. This is not really a problem

I'd suspect you have simply no data. Are you sure you have proper data to display?

Also make sure you're in the UI thread when manipulating RecycleView adapters, so you can also check for that.

breakline
  • 5,776
  • 8
  • 45
  • 84
1

Try this

Java class

Put before on create like this:

   private RecyclerView recycler_view;
    HomeAdapter adapter;

Put inside the onCreate method like this:

recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
adapter = new HomeAdapter ( artists , R.layout.list_item ,getApplicationContext());
recyclerView.setAdapter(adapter);

xml layout

<android.support.v7.widget.RecyclerView
        android:id="@+id/RecyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

adapter class

public class HomeAdapter extends BaseAdapter {
private Context mContext;
private final String[] web;
private final int[] Imageid;

public HomeAdapter(Context c,String[] web,int[] Imageid ) {
    mContext = c;
    this.Imageid = Imageid;
    this.web = web;
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return web.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    View grid;
    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null) {

        grid = new View(mContext);
        grid = inflater.inflate(R.layout.recyclerview_home_layout, null);
        TextView textView = (TextView) grid.findViewById(R.id.company_name);
        ImageView imageView = (ImageView)grid.findViewById(R.id.company_img);
        textView.setText(web[position]);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent in =new Intent(mContext, ServiceActivity.class);
           //                in.putExtra("prop",new Gson().toJson(list.get(position)));
                mContext.startActivity(in);
            }
        });
        imageView.setImageResource(Imageid[position]);
    } else {
        grid = (View) convertView;
    }

    return grid;
}

it helps you try this

Android Geek
  • 8,956
  • 2
  • 21
  • 35