应用程序与抽屉android崩溃

时间:2015-01-29 10:20:39

标签: android

在抽屉外点击时,应用程序崩溃了....

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/flMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/llHeader"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/darker_gray" >

            <ImageButton
                android:id="@+id/ibMenu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:onClick="btnMenu"
                android:src="@drawable/ic_launcher" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center_vertical"
                android:text="@string/app_name"/>
        </LinearLayout>

        <android.support.v4.widget.DrawerLayout
            android:id="@+id/drawer_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/background_light"
            android:layoutDirection="rtl" >

            <ScrollView
                android:id="@+id/llMenuBar"
                android:layout_width="200dp"
                android:layout_height="match_parent"
                android:layout_gravity="start"
                android:choiceMode="singleChoice" >

                <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:orientation="vertical"
                    android:padding="10dp" >

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical" >

                        <Button
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:drawableLeft="@drawable/ic_launcher"
                            android:onClick="btnOption1"
                            android:text="Option 1" />

                        <View
                            android:layout_width="match_parent"
                            android:layout_height="1dp"/>
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical" >

                        <Button
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:drawableLeft="@drawable/ic_launcher"
                            android:onClick="btnOption2"
                            android:text="Option 2" />

                        <View
                            android:layout_width="match_parent"
                            android:layout_height="1dp"/>
                    </LinearLayout>
                </LinearLayout>
            </ScrollView>
        </android.support.v4.widget.DrawerLayout>
    </LinearLayout>

</FrameLayout>

java代码就像......

 private DrawerLayout mDrawerLayout;
    ImageButton ibMenu;
    ScrollView llMenuBar;
    FrameLayout flMain;

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

        setContentView(R.layout.activity_main);
        ibMenu = (ImageButton) findViewById(R.id.ibMenu);
        flMain = (FrameLayout) findViewById(R.id.flMain);
        llMenuBar = (ScrollView) findViewById(R.id.llMenuBar);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerLayout.setScrimColor(Color.parseColor("#80ffffff"));
        mDrawerLayout.setDrawerListener(new RightMenuListener());

        ibMenu.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                openCloseMenu();
            }
        });
        llMenuBar.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                    openCloseMenu();
                }
                return false;
            }
        });
        flMain.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View arg0, MotionEvent event) {
                openCloseMenu();
                return false;
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

            if (isOpen) {
                isOpen = false;
                mDrawerLayout.closeDrawer(llMenuBar);
                ibMenu.setImageResource(R.drawable.ic_launcher);

                return true;
            } else {
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("Exit From App")
                        .setMessage(
                                "Are you sure, you want to leave application?")
                        .setCancelable(false)
                        .setPositiveButton("Yes",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog,
                                            int which) {
                                        moveTaskToBack(true);
                                        System.gc();
                                        android.os.Process
                                                .killProcess(android.os.Process
                                                        .myPid());
                                        System.exit(10);
                                        finish();
                                    }
                                })
                        .setNegativeButton("No",
                                new DialogInterface.OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog,
                                            int which) {

                                    }
                                }).show();
            }

        }
        return super.onKeyDown(keyCode, event);
    }

    public void btnOption1(View v) {
        openCloseMenu();
    }

    public void btnOption2(View v) {
        openCloseMenu();
    }

    boolean isOpen = false;

    public boolean openCloseMenu() {

        if (isOpen) {
            mDrawerLayout.closeDrawer(llMenuBar);
            ibMenu.setImageResource(R.drawable.ic_launcher);
            isOpen = false;
        } else {
            mDrawerLayout.openDrawer(llMenuBar);
            ibMenu.setImageResource(R.drawable.ic_launcher);
            isOpen = true;
        }
        return true;
    }

    private class RightMenuListener implements
            android.support.v4.widget.DrawerLayout.DrawerListener {
        @Override
        public void onDrawerClosed(View view) {
            ibMenu.setImageResource(R.drawable.ic_launcher);
            isOpen = false;
        }

        @Override
        public void onDrawerOpened(View arg0) {
            // TODO Auto-generated method stub
            ibMenu.setImageResource(R.drawable.ic_launcher);
            isOpen = true;
        }

        @Override
        public void onDrawerSlide(View arg0, float arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onDrawerStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }
    }

错误日志就像

android.support.v4.widget.DrawerLayout.isContentView(DrawerLayout.java:840)
at android.support.v4.widget.DrawerLayout.onInterceptTouchEvent(DrawerLayout.java:866)

at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1822)

at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)

at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)


at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)

at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)

at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)

at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)

at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)

at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)

at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)

at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)

at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1925)

at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1379)
at android.app.Activity.dispatchTouchEvent(Activity.java:2396)

at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)

at android.view.View.dispatchPointerEvent(View.java:7307)

at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3172)

at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3117)

at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4153)

at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4132)

at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4224)


at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
01-29 15:42:12.592: E/MessageQueue-JNI(4365):   at android.os.MessageQueue.nativePollOnce(Native Method)
01-29 15:42:12.592: E/MessageQueue-JNI(4365):   at android.os.MessageQueue.next(MessageQueue.java:125)
01-29 15:42:12.592: E/MessageQueue-JNI(4365):   at android.os.Looper.loop(Looper.java:124)
01-29 15:42:12.592: E/MessageQueue-JNI(4365):   at android.app.ActivityThread.main(ActivityThread.java:4745)
01-29 15:42:12.592: E/MessageQueue-JNI(4365):   at java.lang.reflect.Method.invokeNative(Native Method)
01-29 15:42:12.592: E/MessageQueue-JNI(4365):   at java.lang.reflect.Method.invoke(Method.java:511)
01-29 15:42:12.592: E/MessageQueue-JNI(4365):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-29 15:42:12.592: E/MessageQueue-JNI(4365):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-29 15:42:12.592: E/MessageQueue-JNI(4365):   at dalvik.system.NativeStart.main(Native Method)
01-29 15:42:12.592: D/AndroidRuntime(4365): Shutting down VM
01-29 15:42:12.592: W/dalvikvm(4365): threadid=1: thread exiting with uncaught exception (group=0xa624b288)
01-29 15:42:12.612: D/dalvikvm(4365): GC_CONCURRENT freed 184K, 3% free 10945K/11207K, paused 11ms+0ms, total 15ms
01-29 15:42:12.612: E/AndroidRuntime(4365): FATAL EXCEPTION: main
01-29 15:42:12.612: E/AndroidRuntime(4365): java.lang.NullPointerException
01-29 15:42:12.612: E/AndroidRuntime(4365):     at android.support.v4.widget.DrawerLayout.isContentView(DrawerLayout.java:840)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at android.support.v4.widget.DrawerLayout.onInterceptTouchEvent(DrawerLayout.java:866)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1822)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1877)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1925)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1379)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at android.app.Activity.dispatchTouchEvent(Activity.java:2396)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at android.view.View.dispatchPointerEvent(View.java:7307)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3172)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3117)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4153)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4132)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4224)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at android.os.MessageQueue.nativePollOnce(Native Method)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at android.os.MessageQueue.next(MessageQueue.java:125)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at android.os.Looper.loop(Looper.java:124)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at android.app.ActivityThread.main(ActivityThread.java:4745)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at java.lang.reflect.Method.invokeNative(Native Method)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at java.lang.reflect.Method.invoke(Method.java:511)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-29 15:42:12.612: E/AndroidRuntime(4365):     at dalvik.system.NativeStart.main(Native Method)

任何人都可以帮助解决这个问题.....当我按下打开的菜单... app崩溃获得空指针异常..

1 个答案:

答案 0 :(得分:1)

我认为对于抽屉布局,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">

<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textViewContent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />   


</LinearLayout>

<LinearLayout 
    android:id="@+id/dl_left_drawer"
    android:layout_width="300dp"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_gravity="left">


    <TextView
        android:id="@+id/left_drawer_TextView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/left_drawer_TextView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <ListView android:id="@+id/dl_left_drawer_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

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

这里第一个LinearLayout是常规视图,第二个id为dl_left_drawer的是抽屉的布局。

请记住,这些布局可以是任何内容,它们不必是LinearLayout