ActionBarDrawerToggle cannot be applied to Android.support.v7.widget.Toolbar

白昼怎懂夜的黑 提交于 2019-12-08 17:20:40

问题


I keep getting the error saying ActionBarDrawerToggle cannot be applied to v7.widget.Toolbar and because I looked at how others fixed a similar issue they are now both support library files but the error doesn't go away for some reason.

The error says ActionBarDrawerToggle() in ActionBarDrawerToggle cannot be applied to android.support.v7.widget.Toolbar then under actual argument R.id.drawable_ic_drawer (int)

import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.support.v7.widget.Toolbar;

 mDrawerToggle = new android.support.v7.app.ActionBarDrawerToggle(
                getActivity(),                    /* host Activity */
                mDrawerLayout,                    /* DrawerLayout object */
                R.drawable.ic_drawer,             /* nav drawer image to replace 'Up' caret */
                R.string.navigation_drawer_open,  /* "open drawer" description for accessibility */
                R.string.navigation_drawer_close  /* "close drawer" description for accessibility */
        ) {

The part which is said to be making the error is R.drawer.ic_drawer. How do I resolve it?


回答1:


ActionBarDrawerToggle Constructor is as follow.

android.support.v7.app.ActionBarDrawerToggle.ActionBarDrawerToggle(Activity activity,
DrawerLayout drawerLayout, 
Toolbar toolbar, 
int openDrawerContentDescRes, 
int closeDrawerContentDescRes)

You are passing R.drawable.ic_drawer drawable instead of toolbar that's why you are getting this error.

Create a toolbar and add it as action bar and pass this toolbar to this constructor.




回答2:


An alternative way to solve it, is to import import android.support.v4.widget.DrawerLayout instead of import android.support.v7.widget.DrawerLayout

According to the google documentation here: http://developer.android.com/reference/android/support/v4/app/ActionBarDrawerToggle.html



来源:https://stackoverflow.com/questions/29339818/actionbardrawertoggle-cannot-be-applied-to-android-support-v7-widget-toolbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!