我知道有很多类似的问题,但我找不到正确的解决方案。我在我的应用程序中使用了recyclerView。在片段中,我创建RV并设置LayoutManager,如下所示:
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;
}
}
我的XML文件如下所示:
<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>
但我仍然收到错误:
java.lang.IllegalStateException: RecyclerView has no LayoutManager
整个错误:
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 ]
答案 0 :(得分:1)
您的布局管理器应该是LinearLayoutManager。所以,你的代码应该是这样的:
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
recyclerView.setLayoutManager(llm);
答案 1 :(得分:0)
尝试更改
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);