问题
I am getting below exception-
Caused by: java.lang.IllegalStateException: RecyclerView has no LayoutManager
at android.support.v7.widget.RecyclerView.generateLayoutParams(RecyclerView.java:2820)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:757)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at com.peoplecloud.app.guggu.NavigationDrawerFragment.onCreateView(NavigationDrawerFragment.java:75)
at android.app.Fragment.performCreateView(Fragment.java:1700)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:866)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
at android.app.Activity.onCreateView(Activity.java:4803)
I have added the following code,however i am still getting this exception-
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mDrawerList.setLayoutManager(layoutManager);
If I remove the content inside recycler view then there is no exception.
回答1:
Remove the child elements from RecyclerView
in your layout file. I don't know what those are supposed to be, but they are not supposed to be children of the RecyclerView
.
回答2:
mDrawerList.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
mDrawerList.setLayoutManager(llm);
Having setHasFixedSize()
before the LayoutManager
statement fixed the issue for me.
来源:https://stackoverflow.com/questions/30123080/java-lang-illegalstateexception-recyclerview-has-no-layoutmanager