java.lang.IllegalArgumentException:在导航抽屉中找不到重力LEFT的抽屉视图

时间:2015-12-17 03:34:27

标签: java android navigation-drawer android-navigation-drawer

我正在创建一个带导航抽屉的应用。首先它正在工作但是当我编辑我的工具栏的layout_gravity结束时,我收到了一个错误

  

java.lang.IllegalArgumentException:没有发现重力的抽屉视图   LEFT

     

MainActivity

我不知道我的代码中有什么问题,但我只将layout_gravity更改为end。

    public class MainActivity extends AppCompatActivity {

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private CharSequence mTitle;
    private CharSequence mDrawerTitle;
    private ActionBarDrawerToggle mDrawerToggle;
    private Toolbar topToolBar;

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

        /****************************************************************************************
         *                                      DRAWER
         ****************************************************************************************/
        mTitle = mDrawerTitle = " ";

        topToolBar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(topToolBar);
        topToolBar.setLogo(R.drawable.logo);
        topToolBar.setLogoDescription(" ");

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        List<DrawerItem> drawerItems = new ArrayList<DrawerItem>();
        drawerItems.add(new DrawerItem("Home"));
        drawerItems.add(new DrawerItem("Student"));
        drawerItems.add(new DrawerItem("Teacher"));

        mDrawerList.setAdapter(new DrawerAdapter(this, drawerItems));
        mDrawerToggle = new ActionBarDrawerToggle(MainActivity.this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {

            /**
             * Called when a drawer has settled in a completely closed state.
             */
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            /**
             * Called when a drawer has settled in a completely open state.
             */
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };

        // Set the drawer toggle as the DrawerListener
        mDrawerLayout.setDrawerListener(mDrawerToggle);
        mDrawerToggle.setDrawerIndicatorEnabled(true);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                selectItem(position);
            }
        });

    }

    private void selectItem(int position){

        switch(position) {
            default:
            case 0:
                break;
            case 1:
                Intent intent2 = new Intent(MainActivity.this, StudentActivity.class);
                startActivity(intent2);
                finish();
                break;
            case 2:
                Intent intent3 = new Intent(MainActivity.this, TeacherActivity.class);
                startActivity(intent3);
                finish();
                break;
        }

        mDrawerList.setItemChecked(position, true);
        mDrawerLayout.closeDrawer(mDrawerList);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

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

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
  

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ffffff">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="end"
        android:background="#ffffff"
        android:minHeight="?attr/actionBarSize" />

    <android.support.v4.widget.DrawerLayout
        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"
            android:background="#ffffff">


        </LinearLayout>

        <!-- NAVIGATION DRAWER -->
        <ListView android:id="@+id/left_drawer"
            android:layout_width="160dp"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:choiceMode="singleChoice"
            android:divider="#b1b1b1"
            android:dividerHeight="0.5dp"
            android:background="#ffffff"
            />

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

</LinearLayout>
  

logcat错误

FATAL EXCEPTION: main
12-17 11:49:41.253 13881-13881/? E/AndroidRuntime: Process: com.example.jathniel.drawertesting, PID: 13881
12-17 11:49:41.253 13881-13881/? E/AndroidRuntime: java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
12-17 11:49:41.253 13881-13881/? E/AndroidRuntime:     at android.support.v4.widget.DrawerLayout.openDrawer(DrawerLayout.java:1464)
12-17 11:49:41.253 13881-13881/? E/AndroidRuntime:     at android.support.v7.app.ActionBarDrawerToggle.toggle(ActionBarDrawerToggle.java:288)
12-17 11:49:41.253 13881-13881/? E/AndroidRuntime:     at android.support.v7.app.ActionBarDrawerToggle.onOptionsItemSelected(ActionBarDrawerToggle.java:278)
12-17 11:49:41.253 13881-13881/? E/AndroidRuntime:     at com.example.jathniel.drawertesting.MainActivity.onOptionsItemSelected(MainActivity.java:141)
12-17 11:49:41.253 13881-13881/? E/AndroidRuntime:     at android.app.Activity.onMenuItemSelected(Activity.java:3024)
12-17 11:49:41.253 13881-13881/? E/AndroidRuntime:     at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361)

3 个答案:

答案 0 :(得分:2)

添加android:layout_gravity =&#34; left&#34;在你的DrawerLayout中,

答案 1 :(得分:1)

看来ActionBarDrawerToggle期望抽屉与图标位于同一侧。它尝试使用GRAVITY_START打开抽屉(在LTR布局中向左解析,在RTL布局中解析)。

您可以复制ActionBarDrawerToggle代码的来源,并提供一种方法来指定构建它时抽屉所在的一侧,然后使用您的抽屉而不是标准的抽屉。除此之外,我不确定如何最好地解决这个问题,因为被调用的方法是私有的。

答案 2 :(得分:1)

检查以下代码。它正在发挥作用。

<android.support.v4.widget.DrawerLayout  xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
</RelativeLayout>
<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view_left"
    android:layout_height="match_parent"
    android:layout_width="wrap_content"
    android:layout_marginBottom="50dp"
    android:layout_gravity="start"//I think you modify this line
    app:menu="@menu/drawer"/>