3

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

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

4 回答 4

4

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

于 2013-09-24T10:18:51.560 回答
1

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

于 2013-07-06T02:33:31.597 回答
0

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

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

如果是,则删除它们,这应该可以解决。这听起来很奇怪,但它确实有效。:)

类似的帖子在这里

于 2014-04-28T12:29:16.003 回答
0

根据有关创建导航抽屉的 Android Doc,您需要为android.support.v4.widget.DrawerLayout具有两个子视图的布局创建为根:FragmentLayoutListView

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

如果您不需要使用FragmentLayout视图,则可以将其设为visibilityisgone

<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作为第一个 Child for android.support.v4.widget.DrawerLayoutRoot 视图

<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>
于 2014-06-11T12:32:34.643 回答