Error on Recycler view on fragment

本秂侑毒 提交于 2020-07-11 05:01:18

问题


I am using Recyclerview on a fragment when i run the app and open the activity it crashes.I am new to android so i am not able to implement Recyclerview with SQlite as i followed some code on internet and implemented in my code but my app crashes

log

 02-25 11:53:20.362 19942-19942/com.navinkadlak.typroject E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.navinkadlak.typroject, PID: 19942
                                                                       java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.v7.widget.RecyclerView.findViewById(int)' on a null object reference
                                                                           at com.navinkadlak.typroject.Employees.onCreateView(Employees.java:58)
                                                                           at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
                                                                           at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
                                                                           at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
                                                                           at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
                                                                           at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
                                                                           at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
                                                                           at android.support.v4.view.ViewPager.populate(ViewPager.java:1025)
                                                                           at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1545)
                                                                           at android.view.View.measure(View.java:17915)
                                                                           at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
                                                                           at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1692)
                                                                           at android.widget.LinearLayout.measureVertical(LinearLayout.java:760)
                                                                           at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                                                                           at android.view.View.measure(View.java:17915)
                                                                           at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1075)
                                                                           at android.view.View.measure(View.java:17915)
                                                                           at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
                                                                           at android.widget.FrameLayout.onMeasure(FrameLayout.java:465)
                                                                           at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                                           at android.view.View.measure(View.java:17915)
                                                                           at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
                                                                           at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1692)
                                                                           at android.widget.LinearLayout.measureVertical(LinearLayout.java:760)
                                                                           at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                                                                           at android.view.View.measure(View.java:17915)
                                                                           at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
                                                                           at android.widget.FrameLayout.onMeasure(FrameLayout.java:465)
                                                                           at android.view.View.measure(View.java:17915)
                                                                           at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
                                                                           at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1692)
                                                                           at android.widget.LinearLayout.measureVertical(LinearLayout.java:760)
                                                                           at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
                                                                           at android.view.View.measure(View.java:17915)
                                                                           at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5698)
                                                                           at android.widget.FrameLayout.onMeasure(FrameLayout.java:465)
                                                                           at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2731)
                                                                           at android.view.View.measure(View.java:17915)
                                                                           at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2414)
                                                                           at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1416)
                                                                           at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1640)
                                                                           at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1294)
                                                                           at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6614)
                                                                           at android.view.Choreographer$CallbackRecord.run(Choreographer.java:800)
                                                                           at android.view.Choreographer.doCallbacks(Choreographer.java:603)
                                                                           at android.view.Choreographer.doFrame(Choreographer.java:572)
                                                                           at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:786)
                                                                           at android.os.Handler.handleCallback(Handler.java:815)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                           at android.os.Looper.loop(Looper.java:194)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5706)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:372)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(Zygote

my code where it shows error

   helpher=new DatabaseHelper(getActivity());

    dbList= new ArrayList<EmployeeDetails>();
    dbList = helpher.getDataFromDB();


    mRecyclerView = (RecyclerView) mRecyclerView.findViewById(R.id.rv_recycler_view);

    mRecyclerView.setHasFixedSize(true);

    // use a linear layout manager
    mLayoutManager = new LinearLayoutManager(getActivity());
    mRecyclerView.setLayoutManager(mLayoutManager);

    // specify an adapter
    mAdapter = new MyAdapter(getActivity(),dbList);
    mRecyclerView.setAdapter(mAdapter);


    return rootView;

please help I am trying to access the database vale to display into card


回答1:


Replace

mRecyclerView = (RecyclerView) mRecyclerView.findViewById(R.id.rv_recycler_view);

with

mRecyclerView = (RecyclerView) rootView.findViewById(R.id.rv_recycler_view);



来源:https://stackoverflow.com/questions/42452825/error-on-recycler-view-on-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!