当我从 TabFragment 使用 LoaderManager 启动片段时,为啥 Tablayout 和 Viewpager 会因 NullpointerException 而崩溃?

Posted

技术标签:

【中文标题】当我从 TabFragment 使用 LoaderManager 启动片段时,为啥 Tablayout 和 Viewpager 会因 NullpointerException 而崩溃?【英文标题】:Why is Tablayout and Viewpager crashing with NullpointerException when I start a fragment with LoaderManager from TabFragment?当我从 TabFragment 使用 LoaderManager 启动片段时,为什么 Tablayout 和 Viewpager 会因 NullpointerException 而崩溃? 【发布时间】:2016-02-18 17:02:29 【问题描述】:

我正在按照https://androidbelieve.com/navigation-drawer-with-swipe-tabs-using-design-support-library/的教程来实现滑动tablayout。

它非常适合空片段,

但是,我有 3 个带有 LoaderManagers 和 recyclerViews 的片段,每个片段加载不同的数据。当应用程序第一次加载时,如果我在 tabLayout 上从第一个片段单击到第三个片段而不滑动或单击第二个片段,它会崩溃。数据没有加载,因为它实现了 LoaderManager,它从OnLoadFinished() 方法开始实现。我看不到 onCreate().. or onCreateView() 方法已执行。我应该如何解决这个问题?

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_
    android:orientation="vertical"
    android:layout_>


    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        app:tabGravity="center"
        app:tabMode="fixed"
        android:layout_
        android:layout_
        android:fillViewport="false">
    </android.support.design.widget.TabLayout>


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

    </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)

【问题讨论】:

解决了,通过调整offpagelimite value.v 'viewpager.setOffscreenPageLimit(2)'。 但是,这不是永久的方式。按照developer.android.com/reference/android/support/v4/view/…的说法,值应该尽量低.. 【参考方案1】:

简单调整offpagelimite值

viewpager.setOffscreenPageLimit(2), 

因为默认值为 1。

【讨论】:

以上是关于当我从 TabFragment 使用 LoaderManager 启动片段时,为啥 Tablayout 和 Viewpager 会因 NullpointerException 而崩溃?的主要内容,如果未能解决你的问题,请参考以下文章

找不到模块“fs”。使用 simple-json-loader

当我使用 webpack 和 ts-loader 构建项目时,Typescript 环境模块未解析

模块构建失败(来自 ./node_modules/babel-loader/lib/index.js)Vue Js

我的操作系统中的mykernel.iso执行错误

当我尝试从 ckeditor 导入时,“未找到此依赖项”

iPhone MapKit 错误