我見過很多這些問題,但我沒有任何子元素,所以我不知道爲什麼這不能正常工作。我把它從一個活動中移到了一個片段,它不再工作。我看到以前的問題與從回收站查看子元素有關,但這不是這種情況。這裏是我的代碼: 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