0

最近我在我的应用程序中使用了这个功能。但是,使用 ActionBarDrawerToggle 一个新的应用程序,我在那个函数中出现了未使用的错误。所以我实现了 import android.support.v7.app.ActionBarDrawerToggle; 但是现在代码对我不起作用。

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
    }

}

错误在以下代码中:

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

肯定是它在使用 ActionBarDrawerToggle 时更改了语法但找不到。

非常感谢你所做的一切

4

1 回答 1

0

非常感谢你所做的一切。该错误已得到解决。非常感谢@hata 和@Spring Breaker

这是更正的代码,以便其他人可以看到

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);
于 2015-01-13T14:15:07.460 回答