tabhost along with tabs goes when navigation drawer pulled

I have a navigation drawer with swipe views and tab navigation.

My problem is that when navigation drawer is pulled tabs are gone and when i swipe project stops unexpectedly.

Below is the logcat

here is where studio points the error.

   mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                actionBar.setSelectedNavigationItem(position);//error here
            }
        });

05-05 12:00:05.590 19095-19095/com.dot.shreyans.drwaer E/InputEventReceiver﹕ Exception dispatching input event. 05-05 12:00:05.590 19095-19095/com.dot.shreyans.drwaer E/MessageQueue-JNI﹕ Exception in MessageQueue callback: handleReceiveCallback 05-05 12:00:08.100 19095-19095/com.dot.shreyans.drwaer E/MessageQueue-JNI﹕ java.lang.IllegalStateException: setSelectedNavigationIndex not valid for current navigation mode at android.support.v7.internal.app.WindowDecorActionBar.setSelectedNavigationItem(WindowDecorActionBar.java:420) at com.dot.shreyans.drwaer.MainActivity2Activity$1.onPageSelected(MainActivity2Activity.java:69) at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:572) at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:556) at android.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2020) at android.view.View.dispatchTouchEvent(View.java:7248) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903) at android.view.ViewGroup.dispatchTransformedTouchEvent(View Group.java:2174) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953) at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405) at android.app.Activity.dispatchTouchEvent(Activity.java:2410) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901) at android.view.View.dispatchPointerEvent(View.java:7428) at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3223) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3167) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4295) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4274) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4366) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:125) at android.os.Looper.loop(Looper.java:124) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java :511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method) 05-05 12:00:08.100 19095-19095/com.dot.shreyans.drwaer D/AndroidRuntime﹕ Shutting down VM 05-05 12:00:08.100 19095-19095/com.dot.shreyans.drwaer W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x412af930) 05-05 12:00:08.140 19095-19095/com.dot.shreyans.drwaer E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.IllegalStateException: setSelectedNavigationIndex not valid for current navigation mode at android.support.v7.internal.app.WindowDecorActionBar.setSelectedNavigationItem(WindowDecorActionBar.java:420) at com.dot.shreyans.drwaer.MainActivity2Activity$1.onPageSelected(MainActivity2Activity.java:69) at android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:572) at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:556) at android.s upport.v4.view.ViewPager.onTouchEvent(ViewPager.java:2020) at android.view.View.dispatchTouchEvent(View.java:7248) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) at android.view.ViewGroup.d ispatchTransformedTouchEvent(ViewGroup.java:2174) at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917) at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953) at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405) at android.app.Activity.dispatchTouchEvent(Activity.java:2410) at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901) at android.view.View.dispatchPointerEvent(View.java:7428) at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3223) at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3167) at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4295) at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4274) at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4366) at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179) at android.os.MessageQueue.nativePollOnce(Native Method) at android.os.MessageQueue.next(MessageQueue.java:125) at android.os.Looper.loop(Looper.java:124) at android.app.ActivityThread.main(ActivityThread.java:5041) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) at dalvik.system.NativeStart.main(Native Method)

Thanks,

here is xml of activity

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout_test"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity2Activity">


<!-- android:layout_gravity="start" tells DrawerLayout to treat
     this as a sliding drawer on the left side for left-to-right
     languages and on the right side for right-to-left languages.
     If you're not building against API 17 or higher, use
     android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
     the container. -->
<FrameLayout android:id="@+id/container" android:layout_width="match_parent"
    android:layout_height="match_parent" >
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:id="@+id/pager"
    android:layout_width="match_parent" android:layout_height="match_parent"
    tools:context="com.dot.shreyans.drwaer.MainActivity2Activity" /></FrameLayout>
<fragment android:id="@+id/navigation_drawer_test"
    android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
    android:layout_gravity="start"
    android:name="com.dot.shreyans.drwaer.NavigationDrawerFragment"
    tools:layout="@layout/fragment_navigation_drawer" />

here is the listener

mDrawerToggle = new ActionBarDrawerToggle(
                getActivity(),                    /* host Activity */
                mDrawerLayout,                    /* DrawerLayout object */
                R.drawable.ic_drawer,             /* nav drawer image to replace 'Up' caret */
                R.string.navigation_drawer_open,  /* "open drawer" description for accessibility */
                R.string.navigation_drawer_close  /* "close drawer" description for accessibility */
        ) {
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                if (!isAdded()) {
                    return;
                }

                getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
            }

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                if (!isAdded()) {
                    return;
                }

                if (!mUserLearnedDrawer) {
                    // The user manually opened the drawer; store this flag to prevent auto-showing
                    // the navigation drawer automatically in the future.
                    mUserLearnedDrawer = true;
                    SharedPreferences sp = PreferenceManager
                            .getDefaultSharedPreferences(getActivity());
                    sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).commit();
                }

                getActivity().supportInvalidateOptionsMenu(); // calls onPrepareOptionsMenu()
            }
        };

I don't have your full code, but here is a working example

XML

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<!-- Listview to display slider menu -->
<ListView
    android:id="@+id/list_slidermenu"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:choiceMode="singleChoice"
    android:divider="@color/list_divider"
    android:dividerHeight="1dp"       
    android:listSelector="@drawable/list_selector"
    android:background="@color/list_background"/>

Java code

private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;

// nav drawer title
private CharSequence mDrawerTitle;

// used to store app title
private CharSequence mTitle;

// slide menu items
private String[] navMenuTitles;
private TypedArray navMenuIcons;

private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;

SectionsPagerAdapter mSectionsPagerAdapter;
PageFragment[] pageFragments;

/**
 * The {@link ViewPager} that will host the section contents.
 */
ViewPager mViewPager;
int page_position;

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);

     mTitle = mDrawerTitle = getTitle();

    // load slide menu items
    navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);

    // nav drawer icons from resources
    navMenuIcons = getResources()
            .obtainTypedArray(R.array.nav_drawer_icons);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.list_slidermenu);

    navDrawerItems = new ArrayList<NavDrawerItem>();

    // adding nav drawer items to array
    // Home
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
    // Find People
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
    // Photos
    navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));

    // Recycle the typed array
    navMenuIcons.recycle();

    mDrawerList.setOnItemClickListener(new SlideMenuClickListener());

    // setting the nav drawer list adapter
    adapter = new NavDrawerListAdapter(getApplicationContext(),
            navDrawerItems);
    mDrawerList.setAdapter(adapter);

    // enabling action bar app icon and behaving it as toggle button
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, //nav menu toggle icon
            R.string.app_name, // nav drawer open - description for accessibility
            R.string.app_name // nav drawer close - description for accessibility
    ) {
        public void onDrawerClosed(View view) {
            getSupportActionBar().setTitle(mTitle);
            // calling onPrepareOptionsMenu() to show action bar icons
            supportInvalidateOptionsMenu();
        }

        public void onDrawerOpened(View drawerView) {
            getSupportActionBar().setTitle(mDrawerTitle);
            // calling onPrepareOptionsMenu() to hide action bar icons
            supportInvalidateOptionsMenu();
        }
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);


    final ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setOffscreenPageLimit(3);

    // When swiping between different sections, select the corresponding
    // tab. We can also use ActionBar.Tab#select() to do this if we have
    // a reference to the Tab.
    mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            actionBar.setSelectedNavigationItem(position);
            page_position = position;
        }
    });

    // For each of the sections in the app, add a tab to the action bar.
    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        // Create a tab with text corresponding to the page title defined by
        // the adapter. Also specify this Activity object, which implements
        // the TabListener interface, as the callback (listener) for when
        // this tab is selected.
        actionBar.addTab(
                actionBar.newTab()
                        .setText(mSectionsPagerAdapter.getPageTitle(i))
                        .setTabListener(this));
    }

    pageFragments = new PageFragment[mSectionsPagerAdapter.getCount()]; 
}

/**
 * Slide menu item click listener
 * */
private class SlideMenuClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // display view for selected nav drawer item
        MoveToNewActivity(position);
    }
}
链接地址: http://www.djcxy.com/p/18356.html

上一篇: 点击图片时出错

下一篇: 导航抽屉拉出时,tabhost和选项卡一起运行