我正在DrawerLayout
一个新项目中实施新的。我的代码几乎是从DrawerLayout
指南中逐字逐句地找到的,直到我改变方向。方向更改时发生的情况是任何通过设置的视图findViewById
都返回 null。在下面的代码中,方向更改时两者mDrawerList
和mDrawerLayout
都为空,但在第一次打开 Activity 时不是。
我检查空值以防止出现,NullPointerException
但我在 LogCat 中收到另一个警告:
05-22 20:56:08.375: W/PhoneWindow(16528): Previously focused view reported id 2130968626 during save, but can't be found during restore.
Activity 现在加载,但 DrawerLayout 将不再像以前那样从左侧滑出。这里有什么问题?
这是我的 onCreate 调用的方法。
public void initializeDrawer() {
// Get list of menu items from array resource
mDrawerItems = getResources().getStringArray(R.array.nav_drawer_items);
if (mDrawerList == null)
mDrawerList = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the listview
mDrawerList.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_list_item, mDrawerItems));
// Set the lists click listener
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
// Set up the action bar toggle listener to get the draw moving
if (mDrawerLayout == null)
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open,
R.string.drawer_close) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
};
// Attach the listener
mDrawerLayout.setDrawerListener(mDrawerToggle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}