Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 调用方法BottomNavigationView.setOnNavigationItemSelectedListener时出现NullPointerException_Android_Bottomnavigationview - Fatal编程技术网

Android 调用方法BottomNavigationView.setOnNavigationItemSelectedListener时出现NullPointerException

Android 调用方法BottomNavigationView.setOnNavigationItemSelectedListener时出现NullPointerException,android,bottomnavigationview,Android,Bottomnavigationview,当我试图在“切换案例”中使用intent在活动之间切换时,出现以下错误: java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void com.google.android.material.bottomnavigation.BottomNavigationView.setOnNavigationItemSelectedListener(com.google.android.material.BottomNavigationView$OnNavigati

当我试图在“切换案例”中使用intent在活动之间切换时,出现以下错误: java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void com.google.android.material.bottomnavigation.BottomNavigationView.setOnNavigationItemSelectedListener(com.google.android.material.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;
        }
    };

   }

活动\u 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>
<?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>

navigation.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>
<?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);


}

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);


}

根据错误,很明显,
navigation
为空,并且在该空引用上,您正试图
setOnNavigationItemSelectedListener
@ShadowDroid如何确定导航为空。从日志
java.lang.NullPointerException:尝试调用空对象引用上的虚拟方法“…”为很明显,导航是空的。可能你已经更改了id,只是做了即时运行。请进行清洁和重建,然后检查您是否仍然收到相同的错误。根据错误,很明显
导航
为空,并且在该空引用上,您正在尝试
设置导航项SelectedListener
@ShadowDroid导航如何为空。您可以从日志
java.lang.NullPointerException:尝试调用虚拟机空对象引用上的方法“…”明确表示导航为空。可能你已经更改了id,只是做了即时运行。请做一个清理和重建,然后检查您是否仍然得到相同的错误。