调用方法时出现 NullPointerException BottomNavigationView.setOnNavigationItemSelectedListener

NullPointerException while invoking method BottomNavigationView.setOnNavigationItemSelectedListener

当我尝试使用 "switch case" 中的意图在活动之间切换时,出现以下错误: 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 为空,这就是为什么您得到 error.You 正在 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);


}