我正在使用FragmentActivity
实现tabhost,在第一个标签下我加载了一个包含Viewpager
和CirclePageIndIcator
的欢迎片段,我需要在WelcomeFragment旁边加载4个视图
这是我的WelcomeFragment
public class WelcomeFragment extends Fragment
{
PageAdapter mAdapter;
ViewPager mPager;
CirclePageIndicator mIndicator;
List<Fragment> fragments;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_welcome_layout,null);
mPager=(ViewPager)view.findViewById(R.id.myViewPager);
mIndicator=(CirclePageIndicator)view.findViewById(R.id.circlePageIndicator1);
fragments=new ArrayList<Fragment>();
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Fragment fragment;
for(int i=0;i<4;i++)
{
fragment= Fragment.instantiate(getActivity(), TipsFragment.class.getName());
fragments.add(fragment);
}
mAdapter = new PageAdapter(getFragmentManager(),fragments);
mPager.setAdapter(mAdapter);
mIndicator.setViewPager(mPager);
}
}
这是我的页面适配器
public class PageAdapter extends FragmentPagerAdapter implements Serializable{
List<Fragment> fragments;
public PageAdapter(FragmentManager fm,List<Fragment> frags) {
super(fm);
fragments = frags;
}
@Override
public Fragment getItem(int arg0) {
// TODO Auto-generated method stub
return TipsFragment.newInstance(0, 0);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
FragmentManager manager = ((Fragment) object).getFragmentManager();
FragmentTransaction trans = manager.beginTransaction();
trans.remove((Fragment) object);
trans.commit();
super.destroyItem(container, position, object);
}
}
当我首先加载应用程序时,tabFragmentActivity成功加载并且welcomeFragment中的viewpager工作正常,但当我移动到第二个选项卡并再次返回第一个选项卡时,ViewPager
无法成功加载。
我该如何解决?
根据我使用getChildFragmentManager的建议,但app崩溃了 logcat中的错误
09-09 13:28:39.412: E/AndroidRuntime(3100): FATAL EXCEPTION: main
09-09 13:28:39.412: E/AndroidRuntime(3100): java.lang.IllegalStateException: No activity
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1091)
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1877)
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1492)
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:440)
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.os.Handler.handleCallback(Handler.java:725)
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.os.Handler.dispatchMessage(Handler.java:92)
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.os.Looper.loop(Looper.java:137)
09-09 13:28:39.412: E/AndroidRuntime(3100): at android.app.ActivityThread.main(ActivityThread.java:5041)
09-09 13:28:39.412: E/AndroidRuntime(3100): at java.lang.reflect.Method.invokeNative(Native Method)
09-09 13:28:39.412: E/AndroidRuntime(3100): at java.lang.reflect.Method.invoke(Method.java:511)
09-09 13:28:39.412: E/AndroidRuntime(3100): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-09 13:28:39.412: E/AndroidRuntime(3100): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-09 13:28:39.412: E/AndroidRuntime(3100): at dalvik.system.NativeStart.main(Native Method
)
答案 0 :(得分:8)
如果您在ViewPager
中使用Fragment
,则在创建新适配器时,应使用ChildFragmentManager
:
getChildFragmentManager()
答案 1 :(得分:4)
试试这个:
ViewPager pager = [your viewPAger]
pager.setOffscreenPageLimit([quantity of tabs that you are going to use]);
使用该句子(pager.setOffscreenPageLimit),您可以指定标签在被ViewPager删除之前可以站立多远!
干杯!
答案 2 :(得分:3)
使用 AsyncTask 设置ViewPagerAdapter:
private class SetAdapterTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... params) {
return null;
}
@Override
protected void onPostExecute(Void result) {
if(mPagerAdapter != null) mViewPager.setAdapter(mPagerAdapter);
}
}
这样称呼:
mAdapter = new PageAdapter(getChildFragmentManager(), fragments);
new SetAdapterTask().execute();
而不是打电话:
mViewPager.setAdapter(mPagerAdapter);
将适配器设置为 onCreateView(...)
方法,不 onActivityCreated(...).
此外,使用getChildFragmentManager()而不是getFragmentManager()。