我正在新项目中实施新的DrawerLayout
。我的代码几乎是DrawerLayout
指南中的逐字逐句,直到我改变方向才能找到。方向更改时发生的情况是,通过findViewById
设置的任何视图都返回null。在下面的代码中,mDrawerList
和mDrawerLayout
在方向更改时均为null,但在首次打开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.
现在加载活动,但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);
}
答案 0 :(得分:0)
对我来说听起来很奇怪,initializeDrawer()会被调用两次。
如果方法在超类中,那么应该只通过onCreate调用它。为什么子类再次调用initializeDrawer(它应该只调用super(),而super()又调用initializeDrawer)。
您的问题可能是由于在调用initializeDrawer两次时侦听器搞砸了而引起的。
答案 1 :(得分:0)
尝试将以下内容添加到Activity
:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}