gpt4 book ai didi

Android 应用程序因 ActionBarDrawerToggle 而崩溃

转载 作者:行者123 更新时间:2023-11-30 01:40:37 27 4
gpt4 key购买 nike

我正在尝试为我的 MainScreen Activity 设置 Navigation Drawer 监听器。当我设置好所有内容后,当我尝试使用 ActionBarDrawerToggle 时我的应用程序崩溃了。还有其他实现方法吗?

Unable to start activity ComponentInfo{koeksworld.com.firstandroidproject/koeksworld.com.firstandroidproject.UserMainPanel}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.setDrawerListener(android.support.v4.widget.DrawerLayout$DrawerListener)' on a null object reference

这是我 Activity 中的一些代码:

    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user_main_panel);
listener = new NavigationDrawerItemListener(this);
Toolbar toolbar = (Toolbar) findViewById(R.id.groceryAppHomeToolbar);
homeDrawer = (DrawerLayout) findViewById(R.id.mainDrawerLayout);
homeListView = (ListView) findViewById(R.id.homeListView);
homeListView.setBackgroundColor(getResources().getColor(R.color.colorBlue));
navigationArray = getResources().getStringArray(R.array.navigationArray);
navigationAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,
navigationArray);
homeListView.setAdapter(navigationAdapter);
homeListView.setOnItemClickListener(listener);
//The suspected culprit
drawerListener = new ActionBarDrawerToggle(this, homeDrawer, toolbar, R
.string
.open_drawer, R
.string.close_Drawer);
homeDrawer.setDrawerListener(drawerListener);
drawerListener.syncState();
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

}

最佳答案

确保在 findViewById(R.id.mainDrawerLayout) 和布局文件中为 navigation drawer 使用相同的 id。

似乎 mDrawerLayout 在调用 .setDrawerListener() 时为空

关于Android 应用程序因 ActionBarDrawerToggle 而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34555340/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com