Why is Tablayout and Viewpager crashing with NullpointerException when I start a fragment with LoaderManager from TabFragment?

依然范特西╮ 提交于 2019-12-21 17:45:28

问题


I am following the tutorial of https://androidbelieve.com/navigation-drawer-with-swipe-tabs-using-design-support-library/ to implement sliding tablayout.

It works perfect for empty fragments,

However, I have 3 fragments with LoaderManagers and recyclerViews, each one loads different data. When the app loaded at the first time, if I click from the first fragment to the third fragment on the tabLayout without sliding or clicking to second one, it crashes. The data was not loading, as it implements LoaderManager, it started implementing from OnLoadFinished() method. I couldn't see onCreate().. or onCreateView() method was executed. How should I fixed this?

My TabFragment:


public class TabFragment extends Fragment {

    public static TabLayout tabLayout;
    public static ViewPager viewPager;
    public static int int_items = 3 ;
    private Context mContext;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        /**
         *Inflate tab_layout and setup Views.
         */
        View tabView =  inflater.inflate(R.layout.fragment_tab,null);
        tabLayout = (TabLayout) tabView.findViewById(R.id.tabs);
        viewPager = (ViewPager) tabView.findViewById(R.id.viewpager);

        /**
         *Set an Apater for the View Pager
         */
        viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));

        /**
         * Now , this is a workaround ,
         * The setupWithViewPager dose't works without the runnable .
         * Maybe a Support Library Bug .
         */

        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                tabLayout.setupWithViewPager(viewPager);
                for (int i = 0; i < tabLayout.getTabCount(); i++) {
                    TabLayout.Tab tab = tabLayout.getTabAt(i);

                }
            }
        });

        return tabView;

    }

    class MyAdapter extends FragmentPagerAdapter{

        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        /**
         * Return fragment with respect to Position .
         */

        @Override
        public Fragment getItem(int position)
        {
            switch (position){
                case 0 : return new TabFocusFragment();
                case 1 : return new TabFocusFragment();
                case 2 : return new TabFocusFragment();

            }
            return null;
        }

        @Override
        public int getCount() {

            return int_items;

        }


        @Override
        public CharSequence getPageTitle(int position) {

            switch (position){
                case 0 :
                    return "First";
                case 1 :
                    return "Second";
                case 2 :
                    return "Third";
            }
            return null;
        }
    }



    }



TabFocusFragment:

    public class TabFocusFragmentextends extends Fragment
    implements LoaderManager.LoaderCallbacks<Cursor>


    {


    private TabFocusAdapter mAdapter;
    private List<E> mList;

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

        loadData();// never been executed,:(
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tab_focus, container, false);

        loadData();
        setupRecyclerView();
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
    }



        @Override
        public void onLoadMore() {
    if(NeedLoad)
           loadData();
        }

        private void loadData() {
            //Reque

stData
        ……
    }


    private void setupRecyclerView() {
      …...

    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return new CursorLoader(mActivity, ContentProvider.createUri(E.class, null),
                null, null, null, null);
    }

    @Override //fragment starts here, why??
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {


        if (loadedList.size() > 0) {
         //crashes here …...

        if (!fullPacketLoaded) {
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {

    }


TabFragmentLayout:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="wrap_content">


    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        app:tabGravity="center"
        app:tabMode="fixed"
        android:layout_width="match_parent"
        android:layout_height="33dp"
        android:fillViewport="false">
    </android.support.design.widget.TabLayout>


    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </android.support.v4.view.ViewPager>




</LinearLayout>



Errors I got:
> java.lang.NullPointerException: Attempt to invoke interface method
> 'boolean java.util.List.isEmpty()' on a null object reference    at
> com.xxx.xx.fragment.TabFocusFragment.onLoadFinished(TabFocusFragment.java:)
com.xxx.xx.fragment.TabFocusFragment.onLoadFinished(TabFocusFragment.java:)

android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:427)
> android.support.v4.app.LoaderManagerImpl.initLoader(LoaderManager.java:562)
> android.support.v4.app.Fragment.performCreate(Fragment.java:1766)
android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:917)
android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:846)
> android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:860)
android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130)
> android.support.v4.view.ViewPager.populate(ViewPager.java:1103) 11-17
> android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:551)> android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:513)
> android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:494)> android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:1584)
> android.support.design.widget.TabLayout.selectTab(TabLayout.java:807)
> android.support.design.widget.TabLayout.selectTab(TabLayout.java:779)
> android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1039)
> android.support.design.widget.TabLayout$1.onClick(TabLayout.java:620)
> android.view.View.performClick(View.java:4785) 11-17 10:35:42.619
> android.view.View$PerformClick.run(View.java:19858) 11-17 10:35:42.619
> android.os.Handler.handleCallback(Handler.java:739) 11-17 10:35:42.619
> android.os.Handler.dispatchMessage(Handler.java:95) 11-17 10:35:42.619
> android.os.Looper.loop(Looper.java:155) 11-17 10:35:42.619
> android.app.ActivityThread.main(ActivityThread.java:5696) 11-17
> java.lang.reflect.Method.invoke(Native Method) 11-17 10:35:42.619
> java.lang.reflect.Method.invoke(Method.java:372) 11-17 10:35:42.619> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

回答1:


Simplily adjust offpagelimite value

viewpager.setOffscreenPageLimit(2), 

As the default value was 1.



来源:https://stackoverflow.com/questions/33755008/why-is-tablayout-and-viewpager-crashing-with-nullpointerexception-when-i-start-a

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