我试图在RecyclerView
内创建Fragment
。
问题是当我传递上下文时我抓住了NullPointerException
。
这是我在Fragment中的代码:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getActivity().setTitle(getString(R.string.title_services));
String name = "Ivan";
Double price = 25.0;
String duration = "23:30";
String description = "ahjdbsjdhask skdfksd";
items = new ArrayList<>();
for (int i = 0; i < 15; i++) {
items.add(new Service(name, price, duration, description));
}
frameLayout = container.findViewById(R.id.layout_snack);
recyclerView = container.findViewById(R.id.service_list);
itemsAdapter = new ServiceAdapter(R.layout.service_item, items);
fab = container.findViewById(R.id.fab_service);
recyclerView.setLayoutManager(new
LinearLayoutManager(getActivity().getBaseContext()));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(itemsAdapter);
return inflater.inflate(R.layout.fragment_service, container, false);
}
这是我的例外:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: me.ibanha.bizmanager, PID: 3804
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
at me.ibanha.bizmanager.fragment.ServiceFragment.onCreateView(ServiceFragment.java:54)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2343)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1421)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1752)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1821)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2595)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2382)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2337)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2244)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:702)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6519)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
问题在于
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity().getBaseContext()));
我不知道如何在布局管理器中传递上下文。 抱歉我的英语不好。
答案 0 :(得分:1)
您应该在onCreateView
的顶部声明View@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_service, container, false);
getActivity().setTitle(getString(R.string.title_services));
String name = "Ivan";
Double price = 25.0;
String duration = "23:30";
String description = "ahjdbsjdhask skdfksd";
items = new ArrayList<>();
for (int i = 0; i < 15; i++) {
items.add(new Service(name, price, duration, description));
}
frameLayout = view.findViewById(R.id.layout_snack);
recyclerView = view.findViewById(R.id.service_list);
itemsAdapter = new ServiceAdapter(R.layout.service_item, items);
fab = view.findViewById(R.id.fab_service);
recyclerView.setLayoutManager(new
LinearLayoutManager(getActivity().getBaseContext()));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(itemsAdapter);
return view;
}