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

我有一个具有滑动视图和标签导航的导航抽屉。

我的问题是,当导航抽屉拉选项卡消失,当我滑动项目意外停止。

以下是logcat

这里是工作室指出错误的地方。

   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:异常调度输入事件。 05-05 12:00:05.590 19095-19095 / com.dot.shreyans.drwaer E / MessageQueue-JNI:MessageQueue回调中的异常:handleReceiveCallback 05-05 12:00:08.100 19095-19095 / com.dot.shreyans.drwaer E /的MessageQueue-JNI:java.lang.IllegalStateException:setSelectedNavigationIndex无效当前导航模式在android.support.v7.internal.app.WindowDecorActionBar.setSelectedNavigationItem(WindowDecorActionBar.java:420)在com.dot.shreyans.drwaer.MainActivity2Activity $ android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:556)android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:572)处的$ 1.onPageSelected(MainActivity2Activity.java:69) 。android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168)android.view.View.dispatchTouchEvent(View.java:7248)上的.support.v4.view.ViewPager.onTouchEvent(ViewPager.java:2020)。 view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903)at android.view.ViewGroup.dispatchTransformedTouchEvent(View Group.java:2174)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917 )在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)在android.view .ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)在android.view.ViewGroup.dispatchTransformedTouchEvent( ViewGroup.java:2174)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)在com.android.internal.policy.impl.PhoneWindow $ DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)在com.android.internal .policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)at android.view.View.dispatchPointerEvent(View.java:android.app.Activity.dispatchTouchEvent(Activity.java:2410)at com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:1901) 7428)在android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3223)在android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3167)在android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4295)在机器人。 view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4274)在android.view.ViewRootImpl $ WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4366)在android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)在android.os.MessageQueue .nativePollOnce(Native Method)在android.os.MessageQueue.next(MessageQueue.java:125)上android.os.Looper.loop(Looper.java:124)在android.app.ActivityThread.main(ActivityThread.java:5041) )在java.lang.reflect.Method.invoke上的java.lang.reflect.Method.invokeNative(Native Method)(Method.java :511)在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)在dalvik.system.NativeStart。 main(Native Method)05-05 12:00:08.100 19095-19095 / com.dot.shreyans.drwaer D / AndroidRuntime:关闭VM 05-05 12:00:08.100 19095-19095 / com.dot.shreyans.drwaer W / dalvikvm:线程ID = 1:螺纹与未捕获的异常退出(组= 0x412af930)12月5日至5日:00:08.140 19095-19095 / com.dot.shreyans.drwaer E / AndroidRuntime:致命异常:主java.lang.IllegalStateException :setSelectedNavigationIndex不适用于以android.support.v7.internal.app.WindowDecorActionBar.setSelectedNavigationItem(WindowDecorActionBar.java:420)当前导航模式下com.dot.shreyans.drwaer.MainActivity2Activity $ 1.onPageSelected(MainActivity2Activity.java:69) android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:556)at android.s android.support.v4.view.ViewPager.scrollToItem(ViewPager.java:572) 在android.view.android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168)上的android.view.View.dispatchTouchEvent(View.java:7248)上的upport.v4.view.ViewPager.onTouchEvent(ViewPager.java:2020) .ViewGroup.dispatchTouchEvent(ViewGroup.java:1903)在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)在android.view.ViewGroup.dispatchTransformedTouchEvent( ViewGroup.java:2174)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917 )在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)在android.view .ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)at android.view.ViewGroup.d ispatchTransformedTouchEvent(ViewGroup.java:2174)在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)在com.android.internal.policy.impl.PhoneWindow $ DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)在com.android .internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)在android.app.Activity.dispatchTouchEvent(Activity.java:2410)在com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow的.java:1901)在android.view.View.dispatchPointerEvent(View.java:7428)在android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3223)在android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3167)在在android.view.ViewRootImpl $ WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4366)在安卓android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4295)在android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4274)。 view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179) android.os.MessageQueue.next(MessageQueue.java:125)上android.os.Looper.loop(Looper.java:124)android.app.ActivityThread.main上的android.os.MessageQueue.nativePollOnce(Native方法) (ActivityThread.java:5041)在java.lang.reflect.Method.invokeNative(本机方法)在java.lang.reflect.Method.invoke(Method.java:511)在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)

谢谢,

这里是xml的活动

<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" />

这里是听众

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()
            }
        };

我没有完整的代码,但这是一个工作示例

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代码

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/18355.html

上一篇: tabhost along with tabs goes when navigation drawer pulled

下一篇: Can android sdk free your memory before you are done with it?