gpt4 book ai didi

java - 点击底部菜单时如何打开侧面抽屉导航?

转载 作者:行者123 更新时间:2023-12-02 03:04:05 24 4
gpt4 key购买 nike

这就是我想要实现的目标

我想要设置菜单点击将出现侧面抽屉导航

enter image description here

HomeActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);

mMainFrame = (FrameLayout) findViewById(R.id.main_frame);
mMainNav = (BottomNavigationView) findViewById(R.id.main_nav);

homeFragment = new HomeFragment();
analyticsFragment = new AnalyticsFragment();
paymentFragment = new PaymentFragment();
settingsFragment = new SettingsFragment();

drawerLayout = findViewById(R.id.drawerlayout);

FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.main_frame,new HomeFragment()).commit();

mMainNav.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

switch (menuItem.getItemId()) {
case R.id.navigation_home:
setFragment(homeFragment);
return true;

case R.id.navigation_analytics:
setFragment(analyticsFragment);
return true;

case R.id.navigation_payment:
setFragment(paymentFragment);
return true;

case R.id.navigation_settings:
drawerLayout.openDrawer(GravityCompat.END);
return true;

default:
return false;
}
}
});

BottomNavigationView navView = findViewById(R.id.main_nav);
navView.setItemIconTintList(null);

这是我得到的错误

Unable to start activity ComponentInfo{com.example.ewallet/com.example.ewallet.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.app.Activity.findViewById(int)' on a null object reference

编辑:我尝试在此处粘贴一些代码,但 StackOverflow 错误代码太多,无法发布

最佳答案

您没有正确绑定(bind)对象并导致 NullPointerException。

请粘贴您的代码,我们可以为您提供帮助。

我看到了一个奇怪的部分,但也许这个错误仍然会出现,你可能需要粘贴你的layout.xml

mMainNav    =   (BottomNavigationView) findViewById(R.id.main_nav);


BottomNavigationView navView = findViewById(R.id.main_nav);
navView.setItemIconTintList(null);

应修改为

mMainNav.setItemIconTintList(null);

您可以使用

检查它是否为空
getActivity().findViewById(R.id.XXX)

更新

    drawerLayout = (DrawerLayout)getView().findViewById(R.id.drawerlayout);

case R.id.navigation_settings:
drawerLayout.openDrawer(drawerLayout);
return true;

关于java - 点击底部菜单时如何打开侧面抽屉导航?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57032819/

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