导航抽屉关闭bug

时间:2013-06-23 21:01:56

标签: android navigation-drawer

我在打开时创建了一个侧面导航,然后点击所有商品的图标将其关闭。当侧面导航并尝试通过单击其边缘应用程序来关闭它时失败。有什么问题?这是我的代码:

package com.mdev.learnit;

import com.mdev.learnit.helpprogramms.Calculator;
import com.mdev.learnit.settings.SettingsActivity;
import com.mdev.learnit.theory.AlgebraTheoryActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class StartActivity extends Activity {

    final int DIALOG_EXIT = 1;

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

    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    private String[] mPlanetTitles;

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

        mTitle = mDrawerTitle = getTitle();
        mPlanetTitles = getResources().getStringArray(R.array.drawer_names);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);


        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);


        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
               R.layout.drawer_text, mPlanetTitles));


        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  
                mDrawerLayout,        
                R.drawable.ic_drawer,  
                R.string.drawer_open,
                R.string.drawer_close
                ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent intent = new Intent();
       if (mDrawerToggle.onOptionsItemSelected(item)) {
           return true;
       }
       switch(item.getItemId()) {
       case R.id.theory_menu:
           intent.setClass(StartActivity.this, AlgebraTheoryActivity.class);
            startActivity(intent);
           return true;
       case R.id.settings_menu:
           intent.setClass(StartActivity.this, SettingsActivity.class);
            startActivity(intent);
           return true;
       case R.id.calculator_menu:
           intent.setClass(StartActivity.this, Calculator.class);
            startActivity(intent);
           return true;
       default:
           return super.onOptionsItemSelected(item);
       }
   }



    @Override
    public void setTitle(CharSequence title) {
        mTitle = title;
        getActionBar().setTitle(mTitle);
    }


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

        mDrawerToggle.syncState();
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.test, menu);
        return true;
      }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        mDrawerToggle.onConfigurationChanged(newConfig);
    }


    public void onclick(View v) {
          showDialog(DIALOG_EXIT);
        }

    protected Dialog onCreateDialog(int id) {
          if (id == DIALOG_EXIT) {
            AlertDialog.Builder adb = new AlertDialog.Builder(this);
            adb.setTitle(R.string.exit);
            adb.setMessage(R.string.really);
            adb.setIcon(android.R.drawable.ic_dialog_info);
            adb.setPositiveButton(R.string.yes, myClickListener);
            adb.setNegativeButton(R.string.no, myClickListener);
            return adb.create();
          }
          return super.onCreateDialog(id);
        }

    OnClickListener myClickListener = new OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          switch (which) {
          case Dialog.BUTTON_POSITIVE:
            finish();
            break;
          case Dialog.BUTTON_NEGATIVE:          
                    dialog.cancel();
            break;
          }
        }
      };

      public void onBackPressed() {
          showDialog(DIALOG_EXIT);
        }    

}

这是我的日志:

06-24 10:24:48.434: E/InputEventReceiver(373): Exception dispatching input event.
06-24 10:24:48.434: E/MessageQueue-JNI(373): Exception in MessageQueue callback: handleReceiveCallback
06-24 10:24:48.441: E/MessageQueue-JNI(373): java.lang.NullPointerException
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at android.support.v4.widget.DrawerLayout.isContentView(DrawerLayout.java:805)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at android.support.v4.widget.DrawerLayout.onInterceptTouchEvent(DrawerLayout.java:831)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1817)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2236)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1456)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2184)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at android.view.View.dispatchPointerEvent(View.java:7426)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4299)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4278)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4370)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at android.os.MessageQueue.nativePollOnce(Native Method)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at android.os.MessageQueue.next(MessageQueue.java:125)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at android.os.Looper.loop(Looper.java:124)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at android.app.ActivityThread.main(ActivityThread.java:5232)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at java.lang.reflect.Method.invokeNative(Native Method)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at java.lang.reflect.Method.invoke(Method.java:511)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
06-24 10:24:48.441: E/MessageQueue-JNI(373):    at dalvik.system.NativeStart.main(Native Method)
06-24 10:24:48.441: D/AndroidRuntime(373): Shutting down VM
06-24 10:24:48.441: W/dalvikvm(373): threadid=1: thread exiting with uncaught exception (group=0x41f3e888)
06-24 10:24:48.449: E/AndroidRuntime(373): FATAL EXCEPTION: main
06-24 10:24:48.449: E/AndroidRuntime(373): java.lang.NullPointerException
06-24 10:24:48.449: E/AndroidRuntime(373):  at android.support.v4.widget.DrawerLayout.isContentView(DrawerLayout.java:805)
06-24 10:24:48.449: E/AndroidRuntime(373):  at android.support.v4.widget.DrawerLayout.onInterceptTouchEvent(DrawerLayout.java:831)
06-24 10:24:48.449: E/AndroidRuntime(373):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1817)
06-24 10:24:48.449: E/AndroidRuntime(373):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
06-24 10:24:48.449: E/AndroidRuntime(373):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
06-24 10:24:48.449: E/AndroidRuntime(373):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
06-24 10:24:48.449: E/AndroidRuntime(373):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
06-24 10:24:48.449: E/AndroidRuntime(373):  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
06-24 10:24:48.449: E/AndroidRuntime(373):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1875)
06-24 10:24:48.449: E/AndroidRuntime(373):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2236)
06-24 10:24:48.449: E/AndroidRuntime(373):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1456)
06-24 10:24:48.449: E/AndroidRuntime(373):  at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
06-24 10:24:48.449: E/AndroidRuntime(373):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2184)
06-24 10:24:48.449: E/AndroidRuntime(373):  at android.view.View.dispatchPointerEvent(View.java:7426)
06-24 10:24:48.449: E/AndroidRuntime(373):  at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
06-24 10:24:48.449: E/AndroidRuntime(373):  at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
06-24 10:24:48.449: E/AndroidRuntime(373):  at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4299)
06-24 10:24:48.449: E/AndroidRuntime(373):  at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4278)
06-24 10:24:48.449: E/AndroidRuntime(373):  at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4370)
06-24 10:24:48.449: E/AndroidRuntime(373):  at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
06-24 10:24:48.449: E/AndroidRuntime(373):  at android.os.MessageQueue.nativePollOnce(Native Method)
06-24 10:24:48.449: E/AndroidRuntime(373):  at android.os.MessageQueue.next(MessageQueue.java:125)
06-24 10:24:48.449: E/AndroidRuntime(373):  at android.os.Looper.loop(Looper.java:124)
06-24 10:24:48.449: E/AndroidRuntime(373):  at android.app.ActivityThread.main(ActivityThread.java:5232)
06-24 10:24:48.449: E/AndroidRuntime(373):  at java.lang.reflect.Method.invokeNative(Native Method)
06-24 10:24:48.449: E/AndroidRuntime(373):  at java.lang.reflect.Method.invoke(Method.java:511)
06-24 10:24:48.449: E/AndroidRuntime(373):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
06-24 10:24:48.449: E/AndroidRuntime(373):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
06-24 10:24:48.449: E/AndroidRuntime(373):  at dalvik.system.NativeStart.main(Native Method)

4 个答案:

答案 0 :(得分:4)

您可以在主布局中添加FragmentLayout with layout_width/layout_height = match_parent property。如果您不需要,请不要在您的活动中使用/ call FragmentLayout。我知道,这不是一个好的解决方案,但它适用于我。

答案 1 :(得分:1)

我没有看到.closeDrawer()在任何地方被调用。

答案 2 :(得分:0)

我知道这是一个老帖子,但我找到了这个解决方案,它可能会对未来的读者有所帮助。

如果您不希望您的抽屉布局覆盖整个屏幕并仍然解决问题,请检查您的片段布局(在布局xml中)是否添加了任何边距。

如果是,那么删除它们,这应该解决它。这听起来很奇怪,但是它可以解决问题。 :)

类似文章here

答案 3 :(得分:0)

根据Android Doc about create an Navigation Drawer,您需要为包含两个子视图的布局创建android.support.v4.widget.DrawerLayout作为根:FragmentLayoutListView

因为 the XML order implies z-ordering and the drawer must be on top of the content.

如果您不需要使用FragmentLayout视图,则可以visibilitygone

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

      <!-- Framelayout to display Fragments -->
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="gone" />

    <!-- Listview to display slider menu -->
    <ListView
        android:id="@+id/list_slidermenu"
        android:layout_width="match_parent"
        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"/>
</android.support.v4.widget.DrawerLayout>

如果您需要创建多个FragmentLayout,则可以创建RelativeLayoutLinearLayout作为android.support.v4.widget.DrawerLayout根视图的第一个子视图

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

    <RelativeLayout android:layout_width="match_parent"
                    android:layout_height="match_parent" >

        <FrameLayout android:id="@+id/fragment_first"
                     android:layout_width="fill_parent"
                     android:layout_height="wrap_content" >
        </FrameLayout>

        <FrameLayout android:id="@+id/fragment_second"
                     android:layout_width="fill_parent"
                     android:layout_height="fill_parent"
                     android:layout_belo="@id/fragment_first" >

        </FrameLayout>

    </RelativeLayout>

        <!-- Listview to display slider menu -->
        <ListView
            android:id="@+id/list_slidermenu"
            android:layout_width="match_parent"
            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"/>
</android.support.v4.widget.DrawerLayout>