向FragmentPagerAdapter添加第三页时是否存在InflateException?

时间:2016-01-12 14:20:22

标签: android android-fragments fragmentpageradapter

我在support.v4.app.FragmentPagerAdapter中有两个页面的应用程序,事情已经好多年了。现在我想添加第三页,所以我只是重复使用旧页面中的相同代码(当然更改id和类名)。起初似乎工作正常,我可以从1 - 2 - 3滑动,但当我再次刷回第2页时,我得到第1页的InflateException:

01-12 14:55:53.170: E/AndroidRuntime(9080): FATAL EXCEPTION: main
01-12 14:55:53.170: E/AndroidRuntime(9080): Process: com.magnuswikhog.adrdangerousgoods, PID: 9080
01-12 14:55:53.170: E/AndroidRuntime(9080): android.view.InflateException: Binary XML file line #9: Error inflating class fragment
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at com.magnuswikhog.adrlibrary.SearchPageFragment.onCreateView(SearchPageFragment.java:36)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1962)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1426)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:728)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1613)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.support.v4.view.ViewPager$3.run(ViewPager.java:251)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.view.Choreographer.doCallbacks(Choreographer.java:574)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.view.Choreographer.doFrame(Choreographer.java:543)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.os.Handler.handleCallback(Handler.java:733)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.os.Handler.dispatchMessage(Handler.java:95)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.os.Looper.loop(Looper.java:136)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.app.ActivityThread.main(ActivityThread.java:5086)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at java.lang.reflect.Method.invokeNative(Native Method)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at java.lang.reflect.Method.invoke(Method.java:515)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at dalvik.system.NativeStart.main(Native Method)
01-12 14:55:53.170: E/AndroidRuntime(9080): Caused by: java.lang.IllegalArgumentException: Binary XML file line #9: Duplicate id 0x7f0b00b2, tag null, or parent id 0x7f0b00b1 with another fragment for com.magnuswikhog.adrlibrary.SearchMasterFragment
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2289)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:169)
01-12 14:55:53.170: E/AndroidRuntime(9080):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:684)
01-12 14:55:53.170: E/AndroidRuntime(9080):     ... 27 more

这是我的代码:

来自我的FragmentPagerAdapter子类:

    @Override
    public Fragment getItem(int position) {
        Fragment fragment = null;           

        switch( position ){
            case 0:
                fragment = new SearchPageFragment();
                break;
            case 1:                 
                fragment = new LoadPageFragment();
                break;
            case 2:                 
                fragment = new InfoPageFragment();
                break;
        }

        return fragment;
    }

第1页的代码(即SearchPageFragment):

public class SearchPageFragment extends Fragment{

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

        return rootView;
    }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/searchPageFragment" >


        <fragment
            android:id="@+id/searchMasterFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.magnuswikhog.adrlibrary.SearchMasterFragment" />                  


</LinearLayout>

当为第1页的布局调用inflate()时,似乎问题出现在第1页的onCreateView()中。

错误消息中引用的ID是:

    public static int searchMasterFragment=0x7f0b00b2;
    public static int searchPageFragment=0x7f0b00b1;

尝试了几种解决方案&#34;我设法找到谷歌搜索,但到目前为止唯一有用的是简单地使用FragmentStatePagerAdapter - 然后问题就消失了。但我很好奇我将如何解决这个问题,仍然使用FragmentPagerAdapter?

我想在向后滑动时布局再次膨胀,但没有一个解决方案(在再次充气之前移除视图,在onDestroy()中移除片段,将视图存储在成员中并检查它是否&#39;已经!= null等)我发现到目前为止已经能够帮助我防止它了。

顺便说一下,我已经检查过支持库类而不是标准类在任何地方都可以使用。

1 个答案:

答案 0 :(得分:-1)

FragmentPagerAdapter最适合在有少量通常更多的静态片段被分页时使用,例如一组标签。 用户访问的每个页面的片段将保留在内存中,但其视图层次结构可能会在不可见时被销毁。这可能导致使用大量内存,因为片段实例可以保持任意数量的状态。对于较大的页面集,请考虑FragmentStatePagerAdapter。 for more

相关问题
最新问题