问题
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
回答1:
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);
来源:https://stackoverflow.com/questions/27922544/i-have-problems-actionbardrawertoggle