1

我正在DrawerLayout一个新项目中实施新的。我的代码几乎是从DrawerLayout指南中逐字逐句地找到的,直到我改变方向。方向更改时发生的情况是任何通过设置的视图findViewById都返回 null。在下面的代码中,方向更改时两者mDrawerListmDrawerLayout都为空,但在第一次打开 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);

}
4

2 回答 2

0

对我来说,initializeDrawer() 被调用两次听起来很奇怪。

如果该方法在超类中,那么它应该只在那里通过 onCreate 调用。为什么子类再次调用initializeDrawer(它应该只调用super(),然后调用initializeDrawer)。

您的问题可能是因为当您调用 initializeDrawer 两次时侦听器搞砸了。

于 2014-04-16T03:37:34.227 回答
0

尝试将以下内容添加到您的Activity

    @Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}
于 2014-04-16T07:33:50.290 回答