I have a recyclerview that is not found in onCreate, but in onOptionsItemSelected. Here's why: When I click on an item in the options menu, I display a bottom sheet, see: (google specs and the library). The library takes a xml-layout, which is where my recyclerview is located. The problem is that when I'm attaching the layout manager to the recyclerview I get this exception:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollVertically()' on a null object reference at android.support.v7.widget.RecyclerView.computeVerticalScrollRange(RecyclerView.java:1545) at android.view.View.onDrawScrollBars(View.java:12943) at android.view.View.draw(View.java:15237) at android.support.v7.widget.RecyclerView.draw(RecyclerView.java:3037) at android.view.View.updateDisplayListIfDirty(View.java:14167) at android.view.View.getDisplayList(View.java:14189) at android.view.View.draw(View.java:14959) at android.view.ViewGroup.drawChild(ViewGroup.java:3405) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198) at android.view.View.draw(View.java:15234) at android.view.View.updateDisplayListIfDirty(View.java:14167) at android.view.View.getDisplayList(View.java:14189) at android.view.View.draw(View.java:14959) at android.view.ViewGroup.drawChild(ViewGroup.java:3405) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198) at android.view.View.updateDisplayListIfDirty(View.java:14162) at android.view.View.getDisplayList(View.java:14189) at android.view.View.draw(View.java:14959) at android.view.ViewGroup.drawChild(ViewGroup.java:3405) at android.support.v4.widget.DrawerLayout.drawChild(DrawerLayout.java:1089) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198) at android.view.View.updateDisplayListIfDirty(View.java:14162) at android.view.View.getDisplayList(View.java:14189) at android.view.View.draw(View.java:14959) at android.view.ViewGroup.drawChild(ViewGroup.java:3405) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198) at android.view.View.updateDisplayListIfDirty(View.java:14162) at android.view.View.getDisplayList(View.java:14189) at android.view.View.draw(View.java:14959) at android.view.ViewGroup.drawChild(ViewGroup.java:3405) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198) at android.view.View.updateDisplayListIfDirty(View.java:14162) at android.view.View.getDisplayList(View.java:14189) at android.view.View.draw(View.java:14959) at android.view.ViewGroup.drawChild(ViewGroup.java:3405) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198) at android.view.View.updateDisplayListIfDirty(View.java:14162) at android.view.View.getDisplayList(View.java:14189) at android.view.View.draw(View.java:14959) at android.view.ViewGroup.drawChild(ViewGroup.java:3405) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198) at android.view.View.updateDisplayListIfDirty(View.java:14162) at android.view.View.getDisplayList(View.java:14189) at android.view.View.draw(View.java:14959) at android.view.ViewGroup.drawChild(ViewGroup.java:3405) at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3198) at android.view.View.draw(View.java:15234) at android.widget.FrameLayout.draw(FrameLayout.java:598) at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2650) at android.view.View.updateDisplayListIfDirty(View.java:14167) at android.view.View.getDisplayList(View.java:14189) at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:273) at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:279) at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:318) at android.view.ViewRootImpl.draw(ViewRootImpl.java:2530) at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2352) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1982) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767) at android.view.Choreographer.doCallbacks(Choreographer.java:580) at android.view.Choreographer.doFrame(Choreographer.java:550) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreo
So my question is: How to I create a recyclerview in anything else besides onCreate of activity, or onCreateView of fragment?
Any help is much appreciated. Thanks.
Below is my process of creating the recyclerview: First I instantiate the bottom sheet. See code below
private BottomSheetLayout bottomSheet;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
bottomSheet = (BottomSheetLayout) findViewById(R.id.bottomsheet);
}
In onOptionsItemSelected I create the recyclerview. see code below
List<BottomSheetData> mCategoryList = new ArrayList<>();
BottomSheetAdapter bottomAdapter = new BottomSheetAdapter(mCategoryList); //Recyclerview adapter
RecyclerView mRecyclerView = (RecyclerView) bottomSheet.findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.setLayoutManager(new LinearLayoutManager(this)); //error caused here
mRecyclerView.setAdapter(bottomAdapter);
Here is my bottom sheet layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FAF9F7">
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="@android:color/transparent"
android:clipToPadding="false"
android:scrollbars="vertical"
/>
</LinearLayout>