gpt4 book ai didi

android-fragments - 当我从 TabFragment 使用 LoaderManager 启动片段时,为什么 Tablayout 和 Viewpager 会因 NullpointerException 崩溃?

转载 作者:行者123 更新时间:2023-12-04 07:48:11 24 4
gpt4 key购买 nike

我正在关注 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_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)

最佳答案

只需调整 offpagelimite 值

viewpager.setOffscreenPageLimit(2), 

因为默认值为 1。

关于android-fragments - 当我从 TabFragment 使用 LoaderManager 启动片段时,为什么 Tablayout 和 Viewpager 会因 NullpointerException 崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33755008/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com