导航抽屉,关闭特殊区域的bug

时间:2013-08-17 17:23:35

标签: android navigation-drawer

当我触摸这个区域时,我强行关闭: enter image description here 一个人伤心,这里没有.closeDrawer();我不知道,我必须在哪里创造它。 这是我的日志:

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

这是抽屉xml:     

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="270dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="#E3E9E3"
        android:dividerHeight="1dp"
        android:showDividers="middle" 
        android:background="#F3F3F4"/>
</android.support.v4.widget.DrawerLayout>

这是我的代码:

import com.mdev.learnit.helpprogramms.Calculator;
import com.mdev.learnit.settings.AboutActivity;
import com.mdev.learnit.settings.SettingsActivity;

import android.app.Activity;
import android.content.Intent;
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.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class StartActivity extends Activity {

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

        // добавляем тень к открытому Navigation Drawer

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

        // прописываем адаптер к нашему списку

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

        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        // делаем иконку приложения кликабельной

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

        // конфигурируем нашу икноку, добавляем текст для открытия/закрытия, добавляем дополнительное изображение, которое будет обозначать открыт ли Navigation Drawer или же он закрыт

        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  
                mDrawerLayout,        
                R.drawable.ic_drawer_white,  
                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);


    }

    private class DrawerItemClickListener implements ListView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView parent, View view, int position,long id) {
            switch(position) {
        case 0:
                Intent a = new Intent(StartActivity.this, AlgebraTheoryActivity.class);
                startActivity(a);
        break;
        case 1:
                Intent b = new Intent(StartActivity.this, Calculator.class);
               startActivity(b);
        break;
        case 2:
                Intent c = new Intent(StartActivity.this, SettingsActivity.class);
                startActivity(c);
        break;
        case 3:
                Intent d = new Intent(StartActivity.this, AboutActivity.class);
                startActivity(d);
        break;
        default:
            }
        }
   }

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


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

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

    public boolean onOptionsItemSelected(MenuItem item) {
        if (mDrawerToggle.onOptionsItemSelected(item)) {
          return true;
        }
    return super.onOptionsItemSelected(item);
    } 

}

*对不起俄语评论

一个人伤心,这里没有.closeDrawer();我不知道,我必须在哪里创造它。

请帮帮我。

2 个答案:

答案 0 :(得分:1)

根据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视图,则可以让它的可见性消失

<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="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"/>
</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="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"/>
</android.support.v4.widget.DrawerLayout>

答案 1 :(得分:0)

您好我也收到了该错误消息,我的问题是在xml中缺少android.support.v4.widget.DrawerLayout的内容 ejm:

<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:layout_marginTop="50dp" >

<!-- Contenido principal -->

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


 </RelativeLayout>

<!-- Navigation Drawer -->

<ListView
 android:id="@+id/drawer"
 android:layout_width="240dp"
 android:layout_height="match_parent"
 android:layout_gravity="left"
 android:background="#EEE" />

</android.support.v4.widget.DrawerLayout>

在android.support.v4.widget.DrawerLayout中没有那个relativelayout我得到了你的错误,但有了它,就解决了。 我希望这能帮到你