0

Recently I was using in my App this feature. But, using the ActionBarDrawerToggle a new app, I went unused error in that function. So I implemented import android.support.v7.app.ActionBarDrawerToggle; But now the code does not work for me.

public class MainActivity extends ActionBarActivity {
private ListView drawerList;
private String[] drawerOptions;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private Fragment[] fragments = new Fragment[]{
                                               new MainFragment(),
                                               new AcordesFragment(),
                                               new AfinadorFragment(),
                                               new AboutFragment(),
                                               new ExitFragment(),
};

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

    drawerList = (ListView) findViewById(R.id.leftDrawer);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    drawerOptions = getResources().getStringArray(R.array.drawer_options);

    drawerList.setAdapter(new ArrayAdapter<String> (this,
                                                    R.layout.drawer_list_item,
                                                    drawerOptions));

    drawerList.setItemChecked(0, true);
    drawerList.setOnItemClickListener(new DrawerItemClickListener());

    drawerToggle = new ActionBarDrawerToggle(this,
                                            drawerLayout,
                                            R.drawable.ic_drawer,
                                            R.string.drawer_open,
                                            R.string.drawer_close){
                                                                    public void onDrawerClosed(View view){
                                                                        ActivityCompat.invalidateOptionsMenu(MainActivity.this);
                                                                    }
                                                                    public void onDrawerOpened(View drawerView){
                                                                        ActivityCompat.invalidateOptionsMenu(MainActivity.this);
                                                                    }
                                             };

    drawerLayout.setDrawerListener(drawerToggle);

    FragmentManager manager = getSupportFragmentManager();
    manager.beginTransaction()
            .add(R.id.contentFrame, fragments[0])
            .add(R.id.contentFrame, fragments[1])
            .add(R.id.contentFrame, fragments[2])
            .add(R.id.contentFrame, fragments[3])
            .add(R.id.contentFrame, fragments[4])
            .hide(fragments[1])
            .hide(fragments[2])
            .hide(fragments[3])
            .hide(fragments[4])
            .commit();

    ActionBar actionbar = getSupportActionBar();
    actionbar.setDisplayHomeAsUpEnabled(true);
    actionbar.setHomeButtonEnabled(true);

    if( getIntent().getBooleanExtra("Exit me", false)){
        finish();
        return; // add this to prevent from doing unnecessary stuffs
    }

}

The error is in the following code:

    drawerList.setOnItemClickListener(new DrawerItemClickListener());

    drawerToggle = new ActionBarDrawerToggle(this,
                                            drawerLayout,
                                            R.drawable.ic_drawer,
                                            R.string.drawer_open,
                                            R.string.drawer_close){
                                                                    public void onDrawerClosed(View view){
                                                                        ActivityCompat.invalidateOptionsMenu(MainActivity.this);
                                                                    }
                                                                    public void onDrawerOpened(View drawerView){
                                                                        ActivityCompat.invalidateOptionsMenu(MainActivity.this);
                                                                    }
                                             };

Surely Be That it has changed the syntax When using the ActionBarDrawerToggle but can not find.

Thank you very much for everything

Natlum
  • 160
  • 12
  • And what is the error ? post logcat with exact line number. – Spring Breaker Jan 13 '15 at 12:48
  • @Natlum Please refer my Answer: http://stackoverflow.com/questions/27581764/actionbardrawertoggle-no-suitable-constructor-drawable/27583232#27583232 – hata Jan 13 '15 at 12:48
  • @hata Thank you very much for the information. I was able to solve one of the two errors had. But even a bug me is on the line: drawerList.setOnItemClickListener (new DrawerItemClickListener ()); The error message is: Can not resolve symbol DrawerItemClickListener – Natlum Jan 13 '15 at 13:36
  • @Natlum Then you must implement DrawerItemClickListener() at anywhere. I found briefly for example: http://stackoverflow.com/a/20572709/3501958. – hata Jan 13 '15 at 13:50

1 Answers1

0

Thank you very much for everything. The error has been resolved. Thank you very much to @hata and @Spring Breaker

Here is the corrected code so that other people can see it

public class MainActivity extends ActionBarActivity {
private ListView drawerList;
private String[] drawerOptions;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private Fragment[] fragments = new Fragment[]{
                                               new MainFragment(),
                                               new AcordesFragment(),
                                               new AfinadorFragment(),
                                               new AboutFragment(),
                                               new ExitFragment(),
};


private class DrawerItemClickListener implements ListView.OnItemClickListener {

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

    }
};

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

    drawerList = (ListView) findViewById(R.id.leftDrawer);
    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
    drawerOptions = getResources().getStringArray(R.array.drawer_options);

    drawerList.setAdapter(new ArrayAdapter<String> (this,
                                                    R.layout.drawer_list_item,
                                                    drawerOptions));

    drawerList.setItemChecked(0, true);

    drawerList.setOnItemClickListener(new DrawerItemClickListener());

    drawerToggle = new ActionBarDrawerToggle(this,
                                            drawerLayout,
                                            //R.drawable.ic_drawer,
                                            R.string.drawer_open,
                                            R.string.drawer_close){
                                                                    public void onDrawerClosed(View view){
                                                                        ActivityCompat.invalidateOptionsMenu(MainActivity.this);
                                                                    }
                                                                    public void onDrawerOpened(View drawerView){
                                                                        ActivityCompat.invalidateOptionsMenu(MainActivity.this);
                                                                    }
                                             };

    drawerLayout.setDrawerListener(drawerToggle);
Natlum
  • 160
  • 12