ActionBarDrawerToggle No Suitable Constructor Draw

2019-01-23 21:44发布

问题:

I have simple code to create simple navigation drawer, but when i declare parameter for ActionBarDrawerToggle it's say that drawable icon cannot be applied...

Gradle Massages Build

Error:(36, 26) error: no suitable constructor found for     
ActionBarDrawerToggle(MainActivity,DrawerLayout,int,int,int)
constructor ActionBarDrawerToggle.ActionBarDrawerToggle(Activity,DrawerLayout,Toolbar,int,int) is   
not applicable
(argument mismatch; int cannot be converted to Toolbar)
constructor ActionBarDrawerToggle.  
<T>ActionBarDrawerToggle(Activity,Toolbar,DrawerLayout,T,int,int) is not applicable
(cannot infer type-variable(s) T
(actual and formal argument lists differ in length))
where T is a type-variable:
T extends Drawable,DrawerToggle declared in constructor   
<T>ActionBarDrawerToggle(Activity,Toolbar,DrawerLayout,T,int,int)

I dont know where i did wrong, i have see support/v7/widget/Toolbar and ActionBarDrawerToggle but no help

I'm Already did like this question and this

this my import support library

import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

This my Build.Gradle(module:app) dependencies

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:support-v4:21.0.3'
}

This my ActionBarDrawerToggle code

drawerListener = new ActionBarDrawerToggle(this, drawerLayout, R.drawable.ic_drawer,
            R.string.drawer_open, R.string.drawer_close) {

        @Override
        public void onDrawerOpened(View drawerView) {
            Toast.makeText(MainActivity.this, "Drawer Opened",
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            Toast.makeText(MainActivity.this, "Drawer Closed",
                    Toast.LENGTH_SHORT).show();
     }
};


drawerLayout.setDrawerListener(drawerListener);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

This my Android Studio image

回答1:

There are two ActionBarDrawerToggle classes. The support.v4's and the support.v7's. Then, it is very cofusing, the v7's constructor methods are different from v4's.

You may fix it simply by removing the third argument drawerImageRes.

drawerListener = new ActionBarDrawerToggle(
    this,
    drawerLayout,
    // R.drawable.ic_drawer, <== delete this argument
    R.string.drawer_open,
    R.string.drawer_close
    ) {

    @Override
    public void onDrawerOpened(View drawerView) {
        Toast.makeText(MainActivity.this, "Drawer Opened",
                Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDrawerClosed(View drawerView) {
        Toast.makeText(MainActivity.this, "Drawer Closed",
                Toast.LENGTH_SHORT).show();
    }
};


回答2:

Change

import android.support.v4.app.ActionBarDrawerToggle; 

to

import android.support.v7.app.ActionBarDrawerToggle;