我见过很多这些问题,但我没有任何子元素,所以我不知道为什么这不能正常工作。我把它从一个活动中移到了一个片段,它不再工作。我看到以前的问题与从回收站查看子元素有关,但这不是这种情况。这里是我的代码: RecyclerView没有布局管理器
public class TodoListFragment extends Fragment {
@BindView(R.id.todo_recycler_view)
protected RecyclerView mRecyclerView;
@BindView(R.id.fab)
protected FloatingActionButton mFab;
private TodoListAdapter mTodoListAdapter;
private RecyclerView.LayoutManager mLayoutManager;
public static TodoListFragment newInstance() {
return new TodoListFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.todo_list_recycler_view, container, false);
ButterKnife.bind(this, view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
mTodoListAdapter = new TodoListAdapter();
mRecyclerView.setAdapter(mTodoListAdapter);
return view;
}
@OnClick(R.id.fab)
public void onAdd() {
TodoItemView todoItemView = new TodoItemView(getContext());
todoItemView.setText("Schedule a dentist's appointment");
todoItemView.setTime(new Date());
mTodoListAdapter.addItem(todoItemView);
}
}
而这里的todo_list_recycler_view.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/todo_recycler_view"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".presentation.TodoActivity"
tools:showIn="@layout/activity_todo_list" />
任何想法,为什么我得到这个错误?
Caused by: java.lang.IllegalStateException: RecyclerView has no LayoutManager
at android.support.v7.widget.RecyclerView.generateLayoutParams(RecyclerView.java:3393)
at android.view.LayoutInflater.inflate(LayoutInflater.java:502)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at com.vorple.shortlist.shortlist.presentation.TodoListFragment.onCreateView(TodoListFragment.java:44)
如果可能的话,是否可以包含适配器?我需要在我的电脑上运行。 –
当然,这里是:http://pastebin.com/qC40tieM – user3496380
如果你需要模型对象:http://pastebin.com/5GQiwuqf – user3496380