ActionBarDrawerToggle没有合适的构造函数Drawable

时间:2014-12-20 15:43:39

标签: android android-studio navigation-drawer android-appcompat build.gradle

我有简单的代码来创建简单的导航抽屉,但是当我为ActionBarDrawerToggle声明参数时,它说无法应用drawable图标......

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)

我不知道我哪里做错了,我看到support/v7/widget/ToolbarActionBarDrawerToggle但没有帮助

我已经确实喜欢this questionthis

这是我的导入支持库

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;

这是我的Build.Gradle(模块:app)依赖项

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'
}

这是我的ActionBarDrawerToggle代码

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);

这是我的Android Studio image

2 个答案:

答案 0 :(得分:23)

有两个ActionBarDrawerToggle类。 support.v4'ssupport.v7's。 然后,它非常有用,v7的构造方法与v4不同。

您可以通过删除第三个参数 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();
    }
};

答案 1 :(得分:-1)

更改

import android.support.v4.app.ActionBarDrawerToggle; 

import android.support.v7.app.ActionBarDrawerToggle;