0

I know there's a lot of similar questions but I haven't found right solution. I'm using recyclerView in my app. In fragment I create RV and set up LayoutManager like this:

   public class MainModulesFragment extends Fragment {

 Context mContext;
 RecyclerView recyclerView;
ModulItemAdapter mAdapter;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View layout = inflater.inflate(R.layout.fragment_main_module, container, false);
    recyclerView = (RecyclerView) layout.findViewById(R.id.module_list);
    recyclerView.setHasFixedSize(true);
    GridLayoutManager grid = new GridLayoutManager(getActivity().getApplicationContext(), 2);
    recyclerView.setLayoutManager(grid);
    mAdapter = new ModulItemAdapter(getActivity().getApplicationContext(),getData());
    recyclerView.setAdapter(mAdapter);
    return layout;
}

public List<TopMenuButton> getData() {
    //Ponowne załadowanie topMenu
    List<TopMenuButton> menuItems = SessionPreferences.getInstance().getTopMenuItems();
    //createTopMenu(menuItems);
    List<TopMenuButton> newMenuItems = new ArrayList<TopMenuButton>();
    for(TopMenuButton button : menuItems){
        TopMenuButton newButton = new TopMenuButtonSmall(getActivity().getApplicationContext(), button.getItemData());
        newMenuItems.add(newButton);
        if(button == SessionPreferences.getInstance().getSelectedMenuButton())
            SessionPreferences.getInstance().setSelectedMenuButton(newButton);
    }
    recyclerView.removeAllViews();
    /*LinearLayout menuLayout = (LinearLayout)findViewById(R.id.topMenuLayout);
    menuLayout.removeAllViews();*/
    for(TopMenuButton menuButton : newMenuItems){
        menuButton.setMainPanel((MainPanelActivity) getActivity().getApplicationContext());
        recyclerView.addView(menuButton);
    }
    if(menuItems.size() < 5)
        for(int i=0; i < 5 - menuItems.size();i++)
            recyclerView.addView(new TopMenuButtonSmall(getActivity().getApplicationContext(),0));

    SessionPreferences.getInstance().setTopMenuItems(newMenuItems);


    return newMenuItems;

}
}

My XML file looks like this :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:id="@+id/headerLayout"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:background="#119CC7" >

        <ImageView
            android:layout_width="120dp"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dip"
            android:contentDescription="Logo"
            android:layout_alignParentLeft="true"
            android:src="@drawable/main_logo" />

        <TextView
            android:id="@+id/searchTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/logoutTextView"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:drawableTop="@drawable/search"
            android:text="@string/search_button"
            android:textSize="9dip"
            android:textColor="@color/white"/>

        <TextView
            android:id="@+id/addTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@+id/searchTextView"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:drawableTop="@drawable/add"
            android:text="@string/add_button"
            android:textSize="9dip"
            android:textColor="@color/white"/>

        <TextView
            android:id="@+id/logoutTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:drawableTop="@drawable/logout_select"
            android:text="@string/logout_button"
            android:textSize="9dip"
            android:textColor="@color/white"/>

        <TextView
            android:id="@+id/optionsTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_toLeftOf="@+id/addTextView"
            android:drawableTop="@drawable/more"
            android:text="@string/menu_button"
            android:textColor="@color/white"
            android:textSize="9dip"/>

    </RelativeLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="10"
        android:orientation="horizontal">

        <SearchView
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:id="@+id/searchView"
        android:layout_weight="8"
            android:background="@color/white" />
    <ImageView
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:id="@+id/imageView"
        android:layout_weight="2"
        android:background="#e6e6e6" />
    </LinearLayout>
    <android.support.v7.widget.RecyclerView
        android:id="@+id/module_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#D2D5DA">

    </android.support.v7.widget.RecyclerView>
    </LinearLayout>

but still I get an error :

    java.lang.IllegalStateException: RecyclerView has no LayoutManager

WHOLE ERROR :

   Process: mac.baseline.baselinemobile, PID: 14917
    java.lang.NullPointerException
            at android.support.v7.widget.RecyclerView$LayoutManager.removeAndRecycleAllViews(RecyclerView.java:6240)
            at android.support.v7.widget.RecyclerView.setAdapterInternal(RecyclerView.java:592)
            at android.support.v7.widget.RecyclerView.setAdapter(RecyclerView.java:563)
            at mac.baseline.baselinemobile.fragments.MainModulesFragment.onCreateView(MainModulesFragment.java:55)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:454)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:146)
            at android.app.ActivityThread.main(ActivityThread.java:5679)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
            at dalvik.system.NativeStart.main(Native Method)
02-16 18:34:20.199  15288-15288/? E/dalvikvm﹕ >>>>> com.play.rbthd [ userId:0 | appId:10183 ]
02-16 18:34:21.474  15363-15363/? E/dalvikvm﹕ >>>>> com.naviexpert.NaviExpert_Play [ userId:0 | appId:10141 ]
Bartos
  • 1,007
  • 3
  • 15
  • 38

2 Answers2

2

Your layout manager should be LinearLayoutManager. So , your code should look like this :

LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(llm);
karvoynistas
  • 1,295
  • 1
  • 14
  • 30
  • That doesn't matter -- GridLayoutManager is a valid LayoutManager. – Kevin Coppock Feb 16 '16 at 17:29
  • GridLayout is also fine with RV as well – Bartos Feb 16 '16 at 17:34
  • @karvoynistas Nice. But it's amazing to me that almost every article/blog on RecyclerViews has its focus on creating a list, but none on saving the list. It does no good to create a beautifully crafted/valuable list (say a big list of songs I want to download) and then have the list literally be destroyed if I navigate away from the list with the single press of the back button. Why wouldn't saving state code be a requirement for creating every RecyclerView list just like the other requirements: the adapter, the layoutmanager, onCreateViewHolder, onBindViewHolder and getItemCount? Thoughts? – AJW Feb 17 '16 at 03:30
  • You have got true. We have to save sources. If the list is large, It is a valuable save and restore the state of the fragment. May be help http://stackoverflow.com/questions/15313598/once-for-all-how-to-correctly-save-instance-state-of-fragments-in-back-stack – eurosecom Feb 17 '16 at 12:37
  • May be cacheing tools would help. For example http://www.androidhive.info/2014/05/android-working-with-volley-library-1/ – eurosecom Feb 17 '16 at 12:59
0

Try to change

View layout = inflater.inflate(R.layout.fragment_main_module, container, false);
recyclerView = (RecyclerView) layout.findViewById(R.id.module_list);
recyclerView.setHasFixedSize(true);
GridLayoutManager grid = new GridLayoutManager(getActivity(), 2);
recyclerView.setLayoutManager(grid);
eurosecom
  • 2,932
  • 4
  • 24
  • 38
  • Looks good, but it's amazing to me that almost every article on RecyclerViews has its focus on creating a list, but none on saving the list. It does no good to create a beautifully crafted/valuable list (say a big list of songs I want to download) and then have the list literally be destroyed if I navigate away from the list with the single press of the back button. Why wouldn't saving state code be a requirement for creating every RecyclerView list just like the other requirements: the adapter, the layoutmanager, onCreateViewHolder, onBindViewHolder and getItemCount? Thoughts? – AJW Feb 17 '16 at 03:29