始终在平板电脑上打开导航抽屉

时间:2014-03-10 09:17:16

标签: android navigation-drawer

我想在平板电脑中保持导航抽屉打开。要做到这一点,我使用以下代码,但当我触摸内容框架时,我得到空指针异常。

我的XML文件:

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

    <!-- The navigation drawer -->
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="@dimen/drawer_size"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111"/>

</android.support.v4.widget.DrawerLayout>
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="@dimen/drawer_content_padding"/>
</FrameLayout>

Mainactivity:

public class MainActivity extends FragmentActivity {

    private boolean isDrawerLocked = false;
    private ListView drawerList;
    private DrawerLayout drawerLayout;
    private String[] drawerItems;
    private ActionBarDrawerToggle drawerToggle;

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

            drawerList = (ListView) findViewById(R.id.left_drawer);
            drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            FrameLayout frameLayout = (FrameLayout)findViewById(R.id.content_frame);
            if(((ViewGroup.MarginLayoutParams)frameLayout.getLayoutParams()).leftMargin == (int)getResources().getDimension(R.dimen.drawer_size)) {
                drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_OPEN, drawerList);
                drawerLayout.setScrimColor(Color.TRANSPARENT);
                isDrawerLocked = true;
            }

            // Set the adapter for the list view
            drawerItems = getResources().getStringArray(R.array.Guide_elements);

            drawerToggle = new ActionBarDrawerToggle(
                    this,
                    drawerLayout,
                    R.drawable.ic_drawer, 
                    R.string.app_name,  
                    R.string.app_name  
            ) {

                /** Called when a drawer has settled in a completely closed state. */
                public void onDrawerClosed(View view) {
                    getActionBar().setTitle(getTitle());

                    invalidateOptionsMenu();
                }

                /** Called when a drawer has settled in a completely open state. */
                public void onDrawerOpened(View drawerView) {
                    getActionBar().setTitle("Select Option");

                    invalidateOptionsMenu();
                }
            };

            if(!isDrawerLocked) {
                drawerLayout.setDrawerListener(drawerToggle);
            }

            // Set the drawer toggle as the DrawerListener
            DrawerItemClickListener drawerItemClickListener = new DrawerItemClickListener();
            drawerList.setOnItemClickListener(drawerItemClickListener);

            if(!isDrawerLocked) {
                getActionBar().setDisplayHomeAsUpEnabled(true);
            }
        }

        @Override
        protected void onPostCreate(Bundle savedInstanceState) {
            super.onPostCreate(savedInstanceState);
            drawerToggle.syncState();
        }

        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            drawerToggle.onConfigurationChanged(newConfig);
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Pass the event to ActionBarDrawerToggle, if it returns
            // true, then it has handled the app icon touch event
            if (drawerToggle.onOptionsItemSelected(item)) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }

        @Override
        public boolean onPrepareOptionsMenu(Menu menu) {
            // If the nav drawer is open, hide action items related to the content view
            boolean drawerOpen = drawerLayout.isDrawerOpen(drawerList);
            MenuItem item = menu.findItem(R.id.action_settings);
            if(item != null) {
                item.setVisible(!drawerOpen);
            }
            item = menu.findItem(R.id.action_settings);
            if(item != null) {
                item.setVisible(!drawerOpen);
            }
            return super.onPrepareOptionsMenu(menu);
        }

        /**
         * The drawer item click listener
         */
        private class DrawerItemClickListener implements ListView.OnItemClickListener {
            @Override
            public void onItemClick(AdapterView parent, View view, int position, long id) {
                selectItem(position);
            }

            /** Swaps fragments in the main content view */
            private void selectItem(int position) {
                Bundle bundle;
                switch(position) {

                }
                FragmentManager fragmentManager = getFragmentManager();

                fragmentManager.beginTransaction()
                        .replace(R.id.content_frame, null)
                        .commit();

                // Highlight the selected item, update the title, and close the drawer
                drawerList.setItemChecked(position, true);
                setTitle(MainActivity.this.drawerItems[position]);
                if(!isDrawerLocked) {
                    drawerLayout.closeDrawer(drawerList);
                }
            }
        }
    }

但是当我触及content_frame时,我得到空指针异常。如何解决问题以避免空指针异常。

例外详情:

10-30 12:53:42.374: E/InputEventReceiver(3157): Exception dispatching input event.
10-30 12:53:42.374: E/MessageQueue-JNI(3157): Exception in MessageQueue callback: handleReceiveCallback
10-30 12:53:42.414: E/MessageQueue-JNI(3157): java.lang.NullPointerException
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.support.v4.widget.DrawerLayout.isContentView(DrawerLayout.java:853)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.support.v4.widget.DrawerLayout.onInterceptTouchEvent(DrawerLayout.java:879)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1854)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1912)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1912)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1912)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1912)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1966)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1418)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.app.Activity.dispatchTouchEvent(Activity.java:2424)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1914)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.View.dispatchPointerEvent(View.java:7564)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3883)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3778)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5419)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5399)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5370)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5493)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:182)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.os.MessageQueue.nativePollOnce(Native Method)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.os.MessageQueue.next(MessageQueue.java:132)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.os.Looper.loop(Looper.java:124)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at android.app.ActivityThread.main(ActivityThread.java:5103)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at java.lang.reflect.Method.invokeNative(Native Method)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at java.lang.reflect.Method.invoke(Method.java:525)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-30 12:53:42.414: E/MessageQueue-JNI(3157):   at dalvik.system.NativeStart.main(Native Method)
10-30 12:53:42.434: W/dalvikvm(3157): threadid=1: thread exiting with uncaught exception (group=0x41465700)
10-30 12:53:42.484: E/AndroidRuntime(3157): FATAL EXCEPTION: main
10-30 12:53:42.484: E/AndroidRuntime(3157): java.lang.NullPointerException
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.support.v4.widget.DrawerLayout.isContentView(DrawerLayout.java:853)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.support.v4.widget.DrawerLayout.onInterceptTouchEvent(DrawerLayout.java:879)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1854)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1912)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1912)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1912)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2211)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1912)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1966)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1418)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.app.Activity.dispatchTouchEvent(Activity.java:2424)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1914)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.View.dispatchPointerEvent(View.java:7564)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3883)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3778)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3483)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3540)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3429)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3398)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3406)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3379)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5419)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5399)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5370)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5493)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:182)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.os.MessageQueue.nativePollOnce(Native Method)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.os.MessageQueue.next(MessageQueue.java:132)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.os.Looper.loop(Looper.java:124)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at android.app.ActivityThread.main(ActivityThread.java:5103)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at java.lang.reflect.Method.invokeNative(Native Method)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at java.lang.reflect.Method.invoke(Method.java:525)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-30 12:53:42.484: E/AndroidRuntime(3157):     at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:0)

  

如何解决问题以避免空指针异常

我认为你的Naivagtion抽屉里有bcoz。

必须有两个布局。

  1. 当抽屉打开视图时,第一个布局将位于后端 从后面出现

  2. 第二个布局用于前视图,即未打开抽屉时。

  3. 所以,

    尝试在ListView下面的Drawer Layout中添加第二个布局。

    例如

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <!-- The navigation drawer -->
        <ListView
            android:id="@+id/left_drawer"
            android:layout_width="@dimen/drawer_size"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:divider="@android:color/transparent"
            android:dividerHeight="0dp"
            android:background="#111"/>
    
      <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"                  
                    android:text="welcome"         
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textColor="#090" />
    
    </android.support.v4.widget.DrawerLayout>
    

答案 1 :(得分:0)

您需要为ListView drawerItems的字符串数组drawerList创建一个适配器。

答案 2 :(得分:0)

您也可以将抽屉始终打开...只需将其内容放在主布局中,然后移除DrawerLayout。您可以通过providing平板电脑的不同布局资源轻松实现此目的,其中您的抽屉内容直接放置在根布局中。