Home » Android » android – Impossible : No layout manager attached; Skipping layout

android – Impossible : No layout manager attached; Skipping layout

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m completely lost with this bug, I understand it, but I don’t know what’s wrong.
For the code :

// In the OnCreate of my activity
  historyRecyclerView = (RecyclerView)findViewById(R.id.recycler_suggestions);
  SearchBarHistoryAdapter searchBarHistoryAdapter = new SearchBarHistoryAdapter();
  searchBarHistoryAdapter.setActivity(this);
  historyRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
  historyRecyclerView.setAdapter(searchBarHistoryAdapter);
  searchBarDisplayManager.setTypeAdapter(SearchBarDisplayManager.SEARCH_TYPE.HISTORY, searchBarHistoryAdapter);

The SearchDisplayManager just contains a list of adapters.
The adapter :

public class SearchBarHistoryAdapter extends SearchBarAdapter {
private ArrayList<String> historyList;
private HistoryTask historyTask;

private void setHistoryList(ArrayList<String> history) {
historyList = history;
notifyDataSetChanged();
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.search_list_item, parent, false);
final HistoryViewHolder historyViewHolder = new HistoryViewHolder(v);
return historyViewHolder;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
  ((HistoryViewHolder) holder).bind(historyList.get(position));
}

@Override
public int getItemCount() {
return historyList == null ? 0 : historyList.size();
}

@Override
public void startSearch(String searchString) {
if(TextUtils.isEmpty(searchString)) {
  if (historyTask != null) {
    historyTask.cancel(true);
  }
  historyTask = new HistoryTask();
  historyTask.execute();
}else{
  setHistoryList(null);
}
}

private class HistoryTask extends AsyncTask<Void, Void, ArrayList<String>> {

@Override
protected ArrayList<String> doInBackground(Void... params) {
  String history = HelperUI.getSearchbarHistory(activity);
  if (!TextUtils.isEmpty(history)) {
    return new ArrayList<>(Arrays.asList(history.split("--")));
  }

  return new ArrayList<>(0);
}

@Override
protected void onPostExecute(ArrayList<String> results) {
  super.onPostExecute(results);
  if (!results.isEmpty()) {
    setHistoryList(results);
  }
  historyTask = null;
}
}

private class HistoryViewHolder extends RecyclerView.ViewHolder {
TextView hint, name;
ImageView img;

public HistoryViewHolder(View v) {
  super(v);
  name = (TextView) v.findViewById(R.id.app_label);
  hint = ((TextView) v.findViewById(R.id.type_label));
  img = ((ImageView) v.findViewById(R.id.item_icon));
}

public void bind(String suggestion) {
  name.setText(Html.fromHtml(suggestion));
  img.setImageDrawable(activity.getResources().getDrawable(R.drawable.ic_public_grey600_48dp));
  img.setTag(suggestion);
}
}
}

Here is the crazy part, when I update the list in setHistoryList, I know the recycler view has the right adapter and it is not null … but then it kinda loses it, no more adapter in the Recycler view when it tries to update by notifyDatasetChanged() … it just disappears and, of cours, displays nothing.

Any idea what’s wrong ?

How to&Answers:

You Have to Add a layoutmanager as you regularly do with any Adapter you create for a RecyclerView.

 historyRecyclerView.setLayoutManager(new LinearLayoutManager(context));

Just add this line below of code below setAdpater where you are setting your recycler view with the adpapter.

Answer:

You can add this to xml also (Thanks @Indra Kumar S)

app:layoutManager="android.support.v7.widget.LinearLayoutManager"

Answer:

For those using AndroidX, a slight modification of @Doongsil’s solution:

app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"

Answer:

My private Variable here

private RecyclerView recycleViewtask;
private RecyclerView.LayoutManager mLayoutManager;

Then I assign following things

    recycleViewtask = findViewById(R.id.recycleViewtask);

    recycleViewtask.setHasFixedSize(true);
    // use a linear layout manager
    mLayoutManager = new LinearLayoutManager(this);
    recycleViewtask.setLayoutManager(mLayoutManager);