Kabs
Kabs

Reputation: 237

Android Studio Can't find Drawable Resource

package com.sarham.kabs.fruity;

import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
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;


public class MainActivity extends ActionBarActivity implements AdapterView.OnItemClickListener{

    private DrawerLayout drawerLayout;
    private ListView listView;
    private String[] planets;
    private ActionBarDrawerToggle drawerListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
        listView = (ListView)findViewById(R.id.drawerListView);
        planets = getResources().getStringArray(R.array.planets);
        listView.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, planets));
        listView.setOnItemClickListener(this);
        drawerListener = new ActionBarDrawerToggle(this, drawerLayout, R.mipmap.ic_drawer, R.string.drawer_open, R.string.drawer_close){
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                Toast.makeText(MainActivity.this,"Drawer Open", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                Toast.makeText(MainActivity.this, "Drawer Closed", Toast.LENGTH_LONG).show();
            }
        };
        drawerLayout.setDrawerListener(drawerListener);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

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

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this, planets[position]+" Was selected", Toast.LENGTH_LONG).show();
        selectItem(position);
    }
    public void selectItem(int position){
        listView.setItemChecked(position, true);
        setTitle(planets[position]);
    }
    public void setTitle(String title){
        getSupportActionBar().setTitle(title);
    }
}

I'm working on a project in android studio, I'm trying to put the navigation drawer icon but I receive this error: 'cannot find symbol R.mipmap.if_drawer', I've tried placing it in the drawable but the same error is thrown for symbol 'R.drawable. ic_drawer'

After cleaning and rebuliding, I get the following message:

Error:(32, 26) error: no suitable constructor found for ActionBarDrawerToggle(MainActivity,DrawerLayout,int,int,int) constructor ActionBarDrawerToggle.ActionBarDrawerToggle(Activity,Toolbar,DrawerLayout,T,int,int) is not applicable (cannot instantiate from arguments because actual and formal argument lists differ in length) constructor ActionBarDrawerToggle.ActionBarDrawerToggle(Activity,DrawerLayout,Toolbar,int,int) is not applicable (actual argument int cannot be converted to Toolbar by method invocation conversion) constructor ActionBarDrawerToggle.ActionBarDrawerToggle(Activity,DrawerLayout,int,int) is not applicable (actual and formal argument lists differ in length) where T is a type-variable: T extends Drawable,DrawerToggle declared in constructor ActionBarDrawerToggle(Activity,Toolbar,DrawerLayout,T,int,int)

Upvotes: 8

Views: 16310

Answers (4)

Kabs
Kabs

Reputation: 237

on checking through the errors the IDE was throwing and suggestions it gave, the following was what I learnt: there are two constructors for the ActionBarDrawerToggle:

  1. ActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, Toolbar toolbar, int drawerOpenContentDescription, int drawerClosedContentDescription)
  2. ActionBarDrawerToggle(Activity activity, DrawerLayout drawerLayout, int drawerOpenContentDescription, int drawerClosedContentDescription)

I used the 2nd constructor with 4 parameters and the navigation drawer worked fine without placing the 'ic_drawer' icon, instead, it is replaced with a back arrow.

Upvotes: 2

Kiran Benny Joseph
Kiran Benny Joseph

Reputation: 6823

There are two ActionBarDrawerToggle classes. Which is of

  1. v4
  2. v7

It is a common problem of ambiguity.

Solution


Remove third argument from the constructor solves the problem.


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) {
                super.onDrawerOpened(drawerView);
                Toast.makeText(MainActivity.this,"Drawer Open", Toast.LENGTH_LONG).show();
            }

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

After A lot of searching I have this.

Upvotes: 0

Thomas Vos
Thomas Vos

Reputation: 12581

Try to rebuild your project. And do a double check if you got the right name. Uppercases do also count for the name. And what is the extension of the file? If this above didn't help use this code:

drawerListener = new ActionBarDrawerToggle(this, drawerLayout,
                 getDrawable(R.mipmap.ic_drawer),
                 getString(R.string.drawer_open),
                 getString(R.string.drawer_close)){}

Upvotes: 0

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

Please Clean Your Project & Rebuild .

Build > Clean Project Then Build > Rebuild Project

Upvotes: 13

Related Questions