无法在具有Fragment扩展名的类中的onCreateView中创建RecyclerView

时间:2018-06-21 19:35:04

标签: android android-fragments android-recyclerview

我在使用RecyclerView时遇到问题,确切地说,该问题仅在扩展名为Fragment的类的onCreateView中发生。

public class LiveAction extends Fragment {
    ...
    private List<Live> displayList = new ArrayList<>();
    private RecyclerView recyclerView;
    private LiveAdapter lAdapter;

    @Override
        public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);}

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View inflatedView = inflater.inflate(R.layout.content_main, container, false);
        ...
        lAdapter = new LiveAdapter(displayList);
        //here the problem:
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(lAdapter);

我已经尝试将getApplicationContext()替换为getContext(),getActivity()。 GetApplicationContext()和其他方法,最多只能得到另一个错误(例如,使用getActivity()。GetApplicationContext()):“方法调用get'getApplicationContext'可能会产生'java.lang.NullPointerException'...”。 我在哪里错了?

Logicat错误(在S7 Edge上测试)

06-21 22:05:46.015 23990-23990/name.app D/AndroidRuntime: Shutting down VM
06-21 22:05:46.021 23990-23990/name.app E/AndroidRuntime: FATAL EXCEPTION: main
    Process: name.app, PID: 23990
    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 name.app.LiveAction.onCreateView(LiveAction.java:83)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:2346)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1428)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2596)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2383)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
        at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2215)
        at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:649)
        at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:145)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1238)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1086)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1616)
        at android.view.View.measure(View.java:23297)
        at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1212)
        at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:1552)
        at android.view.View.measure(View.java:23297)
        at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1059)
        at android.view.View.measure(View.java:23297)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6928)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:141)
        at android.view.View.measure(View.java:23297)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6928)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
        at android.view.View.measure(View.java:23297)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6928)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:23297)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6928)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
        at android.view.View.measure(View.java:23297)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6928)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:899)
        at android.view.View.measure(View.java:23297)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2844)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1874)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2129)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1743)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7780)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
        at android.view.Choreographer.doCallbacks(Choreographer.java:723)
        at android.view.Choreographer.doFrame(Choreographer.java:658)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
        at android.os.Handler.handleCallback(Handler.java:789)
06-21 22:05:46.022 23990-23990/name.app E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6944)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

0 个答案:

没有答案