Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 不可能:未连接布局管理器;跳过布局_Android - Fatal编程技术网

Android 不可能:未连接布局管理器;跳过布局

Android 不可能:未连接布局管理器;跳过布局,android,Android,我完全被这个错误迷住了,我理解它,但我不知道出了什么问题。 守则: // In the OnCreate of my activity historyRecyclerView = (RecyclerView)findViewById(R.id.recycler_suggestions); SearchBarHistoryAdapter searchBarHistoryAdapter = new SearchBarHistoryAdapter(); searchBarHistoryAd

我完全被这个错误迷住了,我理解它,但我不知道出了什么问题。 守则:

// 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);
SearchDisplayManager
只包含适配器列表。 适配器:

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);
}
}
}
公共类SearchBarHistoryAdapter扩展了SearchBarAdapter{
私人ArrayList historyList;
私有历史任务历史任务;
私有void setHistoryList(ArrayList历史){
历史主义者=历史;
notifyDataSetChanged();
}
@凌驾
public RecyclerView.ViewHolder onCreateViewHolder(视图组父级,int-viewType){
视图v=LayoutInflater.from(parent.getContext()).flate(R.layout.search\u list\u项,parent,false);
最终HistoryViewHolder HistoryViewHolder=新的HistoryViewHolder(v);
返回历史视图持有者;
}
@凌驾
BindViewHolder上的公共无效(RecyclerView.ViewHolder,int位置){
((HistoryViewHolder)holder.bind(historyList.get(position));
}
@凌驾
public int getItemCount(){
返回historyList==null?0:historyList.size();
}
@凌驾
public void startSearch(字符串搜索字符串){
if(TextUtils.isEmpty(searchString)){
if(historyTask!=null){
historyTask.cancel(true);
}
historyTask=新的historyTask();
execute();
}否则{
setHistoryList(空);
}
}
私有类HistoryTask扩展了AsyncTask{
@凌驾
受保护的ArrayList doInBackground(无效…参数){
String history=HelperUI.getSearchbarHistory(活动);
如果(!TextUtils.isEmpty(历史)){
返回新的ArrayList(Arrays.asList(history.split(“--”));
}
返回新的ArrayList(0);
}
@凌驾
受保护的void onPostExecute(ArrayList结果){
super.onPostExecute(结果);
如果(!results.isEmpty()){
setHistoryList(结果);
}
historyTask=null;
}
}
私有类HistoryViewHolder扩展了RecyclerView.ViewHolder{
TextView提示,名称;
图像视图img;
公共历史视图持有者(视图v){
超级(五);
name=(TextView)v.findViewById(R.id.app_标签);
提示=((文本视图)v.findviewbyd(R.id.type_标签));
img=((ImageView)v.findViewById(R.id.item_图标));
}
公共无效绑定(字符串建议){
name.setText(Html.fromHtml(建议));
setImageDrawable(activity.getResources().getDrawable(R.drawable.ic_public_grey600_48dp));
img.setTag(建议);
}
}
}
这里是疯狂的部分,当我在
setHistoryList
中更新列表时,我知道回收器视图有正确的适配器,它不是
null
。。。但是,当它尝试通过
notifyDatasetChanged()
进行更新时,它有点丢失了它,在回收器视图中没有更多的适配器。。。它只是消失了,当然,什么也没有显示


你知道怎么了吗?

你必须添加一个layoutmanager,就像你为RecyclerView创建的任何适配器一样

 historyRecyclerView.setLayoutManager(new LinearLayoutManager(context));

只需在下面的代码
setAdpater
中添加这一行,即可使用adapter设置回收器视图

您也可以将其添加到xml中(谢谢@Indra Kumar S)


这里是我的私有变量

private RecyclerView recycleViewtask;
private RecyclerView.LayoutManager mLayoutManager;
然后我分配以下内容

    recycleViewtask = findViewById(R.id.recycleViewtask);

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

对于使用AndroidX的用户,对@Doongsil的解决方案稍加修改:

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

这听起来可能很疯狂,但是试试这个,将你的layoutmanager、recyclerview和adapter声明为类级变量。非常感谢,它起了作用,这让我知道了为什么它不起作用。setAdapter(searchBarHistoryAdapter);searchBarDisplayManager.setTypeAdapter(searchBarDisplayManager.SEARCH_TYPE.HISTORY,searchBarHistoryAdapter);这样做并使用存储在displayManager中的适配器是不起作用的,因为回收器视图失去了引用(gc因为未使用),所以如果我交换这两行代码,使回收器视图有一个对适配器的引用,而该适配器不会得到gc.app:layoutManager=“android.support.v7.widget.LinearLayoutManager”@amin。。。我很高兴我的回答有帮助:)显然,Android Studio 4.1在拖放组件树时没有自动添加此属性,这让我头痛了一段时间。
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"