不止一次切换到另一个标签会给出EEROR

时间:2016-05-06 07:16:21

标签: android tabs android-viewpager

我有一个标签式活动。 当我从tab1切换到tab2或3时,应用程序运行顺畅。 我再次切换到tab1,然后切换到任何其他选项卡,应用程序崩溃。

选项卡式活动java代码

public class MainActivity extends AppCompatActivity {
    ViewPager vp;
    TabLayout tab;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vp= (ViewPager) findViewById(R.id.viewpager);
        tab= (TabLayout) findViewById(R.id.tab);
        vp.setAdapter(new CustomAdapter(getSupportFragmentManager(),MainActivity.this));
        tab.setupWithViewPager(vp);
        tab.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

                vp.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }
    private class CustomAdapter extends FragmentPagerAdapter
    {
        private String fragments[]={"District","NearBy","Search"};
        public CustomAdapter(FragmentManager supportFragmentManager, Context applicationContext)
        {
            super(supportFragmentManager);
        }

        @Override
        public Fragment getItem(int position)
        {
            switch (position)
            {
                case 0:
                    return new MqmDList();
                case 1:
                    return new MqmLList();
                case 2:
                    return new Mqm_MList();
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return fragments.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return fragments[position];
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)  {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getRepeatCount() == 0) {
            Intent homeIntent = new Intent(Intent.ACTION_MAIN);
            homeIntent.addCategory( Intent.CATEGORY_HOME );
            homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(homeIntent);
        }
        return super.onKeyDown(keyCode, event);
    }
}

错误

05-06 12:37:38.380 3356-3356/soft.yu.com.maqam E/Google Maps Android API: Google Maps Android API v2 only supports devices with OpenGL ES 2.0 and above
05-06 12:37:40.451 3356-3560/soft.yu.com.maqam E/DynamiteModule: Failed to load module descriptor class: Didn't find class "com.google.android.gms.dynamite.descriptors.com.google.android.gms.googlecertificates.ModuleDescriptor" on path: DexPathList[[zip file "/data/app/soft.yu.com.maqam-1/base.apk"],nativeLibraryDirectories=[/data/app/soft.yu.com.maqam-1/lib/x86, /system/lib, /vendor/lib]]
05-06 12:37:48.548 3356-3356/soft.yu.com.maqam E/AndroidRuntime: FATAL EXCEPTION: main
                                                                 Process: soft.yu.com.maqam, PID: 3356
                                                                 android.view.InflateException: Binary XML file line #5: Binary XML file line #5: Error inflating class fragment
                                                                 Caused by: android.view.InflateException: Binary XML file line #5: Error inflating class fragment
                                                                 Caused by: java.lang.IllegalArgumentException: Binary XML file line #5: Duplicate id 0x7f0d0081, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment
                                                                     at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2293)
                                                                     at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                                     at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:189)
                                                                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
                                                                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
                                                                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
                                                                     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                                                                     at soft.yu.com.maqam.Mqm_MList.onCreateView(Mqm_MList.java:67)
                                                                     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
                                                                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                                     at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1430)
                                                                     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:732)
                                                                     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
                                                                     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
                                                                     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
                                                                     at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
                                                                     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:608)
                                                                     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:570)
                                                                     at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:551)
                                                                     at soft.yu.com.maqam.MainActivity$1.onTabSelected(MainActivity.java:29)
                                                                     at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1025)
                                                                     at android.support.design.widget.TabLayout.selectTab(TabLayout.java:995)
                                                                     at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1272)
                                                                     at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:1377)
                                                                     at android.view.View$PerformClick.run(View.java:22215)
                                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                     at android.os.Looper.loop(Looper.java:148)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:6066)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:770)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:660)
05-06 12:39:06.671 4725-4725/soft.yu.com.maqam E/Google Maps Android API: Google Maps Android API v2 only supports devices with OpenGL ES 2.0 and above
05-06 12:39:08.120 4725-4725/soft.yu.com.maqam E/SurfaceTexture: isProtectedSurface: invalid current EGLDisplay

                                                                 [ 05-06 12:39:08.122  4725: 4826 D/         ]
                                                                 HostConnection::get() New Host Connection established 0xa9457af0, tid 4826
05-06 12:39:08.736 4725-4828/soft.yu.com.maqam E/DynamiteModule: Failed to load module descriptor class: Didn't find class "com.google.android.gms.dynamite.descriptors.com.google.android.gms.googlecertificates.ModuleDescriptor" on path: DexPathList[[zip file "/data/app/soft.yu.com.maqam-1/base.apk"],nativeLibraryDirectories=[/data/app/soft.yu.com.maqam-1/lib/x86, /system/lib, /vendor/lib]]
05-06 12:39:11.825 4725-4725/soft.yu.com.maqam E/AndroidRuntime: FATAL EXCEPTION: main
                                                                 Process: soft.yu.com.maqam, PID: 4725
                                                                 android.view.InflateException: Binary XML file line #5: Binary XML file line #5: Error inflating class fragment
                                                                 Caused by: android.view.InflateException: Binary XML file line #5: Error inflating class fragment
                                                                 Caused by: java.lang.IllegalArgumentException: Binary XML file line #5: Duplicate id 0x7f0d0081, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment
                                                                     at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2293)
                                                                     at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                                     at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:189)
                                                                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
                                                                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
                                                                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
                                                                     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                                                                     at soft.yu.com.maqam.Mqm_MList.onCreateView(Mqm_MList.java:67)
                                                                     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
                                                                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                                     at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1430)
                                                                     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:732)
                                                                     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
                                                                     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
                                                                     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
                                                                     at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
                                                                     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:608)
                                                                     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:570)
                                                                     at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:551)
                                                                     at soft.yu.com.maqam.MainActivity$1.onTabSelected(MainActivity.java:29)
                                                                     at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1025)
                                                                     at android.support.design.widget.TabLayout.selectTab(TabLayout.java:995)
                                                                     at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1272)
                                                                     at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:1377)
                                                                     at android.view.View$PerformClick.run(View.java:22215)
                                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                     at android.os.Looper.loop(Looper.java:148)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:6066)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:770)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:660)
05-06 12:39:20.267 4948-4948/soft.yu.com.maqam E/Google Maps Android API: Google Maps Android API v2 only supports devices with OpenGL ES 2.0 and above
05-06 12:39:20.388 4948-4948/soft.yu.com.maqam E/SurfaceTexture: isProtectedSurface: invalid current EGLDisplay

                                                                 [ 05-06 12:39:20.388  4948: 5066 D/         ]
                                                                 HostConnection::get() New Host Connection established 0xab02c370, tid 5066
05-06 12:39:22.332 4948-5070/soft.yu.com.maqam E/DynamiteModule: Failed to load module descriptor class: Didn't find class "com.google.android.gms.dynamite.descriptors.com.google.android.gms.googlecertificates.ModuleDescriptor" on path: DexPathList[[zip file "/data/app/soft.yu.com.maqam-1/base.apk"],nativeLibraryDirectories=[/data/app/soft.yu.com.maqam-1/lib/x86, /system/lib, /vendor/lib]]
05-06 12:39:25.434 4948-4948/soft.yu.com.maqam E/AndroidRuntime: FATAL EXCEPTION: main
                                                                 Process: soft.yu.com.maqam, PID: 4948
                                                                 android.view.InflateException: Binary XML file line #5: Binary XML file line #5: Error inflating class fragment
                                                                 Caused by: android.view.InflateException: Binary XML file line #5: Error inflating class fragment
                                                                 Caused by: java.lang.IllegalArgumentException: Binary XML file line #5: Duplicate id 0x7f0d0081, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.maps.SupportMapFragment
                                                                     at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2293)
                                                                     at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                                     at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:189)
                                                                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:769)
                                                                     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
                                                                     at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
                                                                     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
                                                                     at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                                                                     at soft.yu.com.maqam.Mqm_MList.onCreateView(Mqm_MList.java:67)
                                                                     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
                                                                     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
                                                                     at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1430)
                                                                     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:732)
                                                                     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
                                                                     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
                                                                     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
                                                                     at android.support.v4.view.ViewPager.populate(ViewPager.java:1177)
                                                                     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:608)
                                                                     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:570)
                                                                     at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:551)
                                                                     at soft.yu.com.maqam.MainActivity$1.onTabSelected(MainActivity.java:29)
                                                                     at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1025)
                                                                     at android.support.design.widget.TabLayout.selectTab(TabLayout.java:995)
                                                                     at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1272)
                                                                     at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:1377)
                                                                     at android.view.View$PerformClick.run(View.java:22215)
                                                                     at android.os.Handler.handleCallback(Handler.java:739)
                                                                     at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                     at android.os.Looper.loop(Looper.java:148)
                                                                     at android.app.ActivityThread.main(ActivityThread.java:6066)
                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:770)
                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:660)

我有一个标签式活动。 当我从tab1切换到tab2或3时,应用程序运行顺畅。 我再次切换到tab1,然后切换到任何其他选项卡,应用程序崩溃。 我有一个标签活动。

当我从tab1切换到tab2或3时,应用程序运行顺畅。 我再次切换到tab1,然后切换到任何其他选项卡,应用程序崩溃。

1 个答案:

答案 0 :(得分:1)

更改标签时,您必须删除地图片段。它对我有用;)

@Override public void onDestroyView() {
     super.onDestroyView();

     FragmentManager fm = getActivity().getSupportFragmentManager();
     Fragment fragment = (fm.findFragmentById(R.id.mapd));
     FragmentTransaction ft = fm.beginTransaction();
     ft.remove(fragment);
     ft.commit();
 }