gpt4 book ai didi

android - 调用方法 BottomNavigationView.setOnNavigationItemSelectedListener 时出现 NullPointerException

转载 作者:行者123 更新时间:2023-11-30 04:54:33 25 4
gpt4 key购买 nike

当我尝试使用“切换案例”中的 Intent 在 Activity 之间切换时,出现以下错误:java.lang.NullPointerException:尝试在空对象引用上调用虚方法“void com.google.android.material.bottomnavigation.BottomNavigationView.setOnNavigationItemSelectedListener(com.google.android.material.bottomnavigation.BottomNavigationView$OnNavigationItemSelectedListener)”

MainActivity.java



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

BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation2);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);


}

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

switch (item.getItemId()) {
case R.id.ic_home:
return true;
case R.id.ic_wallet:
Intent intent_wallet = new Intent(MainActivity.this,Wallet.class);
startActivity(intent_wallet);
return true;
case R.id.ic_status:
Intent intent_status = new Intent(MainActivity.this,Status.class);
startActivity(intent_status);
return true;
case R.id.ic_history:
Intent intent_history = new Intent(MainActivity.this,History.class);
startActivity(intent_history);
return true;
}
return false;
}
};

}

activity_main.xml

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.praveen.gupta.frontpage.MainActivity">

<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navigation2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@drawable/white_grey_border_top"
app:labelVisibilityMode="labeled"
app:menu="@menu/navigation" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
>

<item
android:id="@+id/ic_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="Home"
/>

<item
android:id="@+id/ic_wallet"
android:icon="@drawable/ic_account_balance_wallet_black_24dp"
android:title="Wallet"
/>
<item
android:id="@+id/ic_status"
android:icon="@drawable/ic_star_border_black_24dp"
android:title="Status"
/>
<item
android:id="@+id/ic_history"
android:icon="@drawable/ic_history_black_24dp"
android:title="History"
/>
</menu>

最佳答案

mOnNavigationItemSelectedListener 为空,这就是您收到错误的原因。您正在 oncreate 方法之外进行初始化。

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//move `mOnNavigationItemSelectedListener` code here .
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

switch (item.getItemId()) {
case R.id.ic_home:
return true;
case R.id.ic_wallet:
Intent intent_wallet = new Intent(MainActivity.this,Wallet.class);
startActivity(intent_wallet);
return true;
case R.id.ic_status:
Intent intent_status = new Intent(MainActivity.this,Status.class);
startActivity(intent_status);
return true;
case R.id.ic_history:
Intent intent_history = new Intent(MainActivity.this,History.class);
startActivity(intent_history);
return true;
}
return false;
}
};

BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation2);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);


}

关于android - 调用方法 BottomNavigationView.setOnNavigationItemSelectedListener 时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59465467/

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