我正在研究android Navigation Drawer
,通过他们的文档看起来,抽屉只能扩展Fragment Activity,因此要从我的所有活动中打开抽屉,我需要将我的所有活动都变成一个片段,这不是一个可行的解决方案。
有没有办法可以打开一个从 Activity 扩展 FragmentActivity 的抽屉?
当我尝试从 Activity 类扩展我的抽屉活动时,另一个将打开抽屉的活动扩展抽屉活动类(此处为 SlideMenuActivity),应用程序崩溃并给出 NullPointerException。
下面是打开抽屉布局的代码,但是一旦第一个活动启动,我就无法访问抽屉。
应用程序在 onPostCreate 方法中的 syncState 点崩溃
@Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
getActionDrawerToggle().syncState();
}
public class SlideMenuActivity extends FragmentActivity implements OnItemClickListener
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setDrawerLayout();
setDrawerList();
if (savedInstanceState == null)
{
getDrawerListView().setSelectionAfterHeaderView();
getDrawerListView().setSelection(1);
selectItem(1);
}
}
//
// @Override
// public void setContentView(int layoutResID) {
// // TODO Auto-generated method stub
// super.setContentView(layoutResID);
// }
private DrawerLayout getDrawerView()
{
return (DrawerLayout)findViewById(R.id.drawer_layout);
}
private ListView getDrawerListView()
{
return (ListView) findViewById(R.id.left_drawer);
}
private ActionBarDrawerToggle getActionDrawerToggle()
{
ActionBarDrawerToggle drawerToggle=new ActionBarDrawerToggle(
this, /* host Activity */
getDrawerView(), /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description for accessibility */
R.string.drawer_close /* "close drawer" description for accessibility */
) {
@Override
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
@Override
public void onDrawerOpened(View view) {
super.onDrawerOpened(view);
}
};
return drawerToggle;
}
private void setDrawerLayout(){
// set a custom shadow that overlays the main content when the drawer opens
getDrawerView().setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.RELATIVE_HORIZONTAL_GRAVITY_MASK);
// enable ActionBar app icon to behave as action to toggle navigation drawer
getDrawerView().setDrawerListener(getActionDrawerToggle());
}
/**
* Set up the drawer's list view with items and click listener
*/
private void setDrawerList()
{
ImageView imageView=new ImageView(this);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.precision_logo));
CustomBaseAdapter adapter=new CustomBaseAdapter();
adapter.list=getListViewData();
adapter.context=this;
ListView drawerList=getDrawerListView();
drawerList.setHeaderDividersEnabled(true);
drawerList.addHeaderView(imageView, null, false);
drawerList.setScrollingCacheEnabled(false);
drawerList.setAdapter(adapter);
drawerList.setOnItemClickListener(this);
}
private void selectItem(int position){
// update the main content by replacing fragments
// Fragment fragment=null;
// FragmentManager manager=getSupportFragmentManager();
switch (position) {
case 1:
this.startActivity(new Intent(this,SavedTankListActivity.class));
// fragment = new SavedMixesFragment();
// fragment = new SavedTankListActivity();
// manager.beginTransaction().replace(R.id.content_frame, fragment).commit();
break;
case 2:
// fragment=new MixGuideFragment();
// manager.beginTransaction().replace(R.id.content_frame, fragment).commit();
break;
default:
// fragment = new SavedMixesFragment();
// manager.beginTransaction().replace(R.id.content_frame, fragment).commit();
break;
}
getDrawerView().closeDrawer(getDrawerListView());
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onPostCreate(savedInstanceState);
getActionDrawerToggle().syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
getActionDrawerToggle().onConfigurationChanged(newConfig);
}
private ArrayList<DrawerListModel> getListViewData()
{
ArrayList<DrawerListModel> listViewData=new ArrayList<DrawerListModel>();
String[] listItemArray=getResources().getStringArray(R.array.slide_bar_list_item_array);
for(int index=0;index<listItemArray.length;index++)
{
DrawerListModel model=new DrawerListModel();
model.listItem=listItemArray[index];
listViewData.add(model);
}
return listViewData;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id)
{
view.setSelected(true);
selectItem(position);
}
public void openDrawerList(View view){
getDrawerView().openDrawer(getDrawerListView());
}
}