javaandroidnullbottomnavigationviewfindviewbyid

bottomNavigationView = findViewById(R.id.navigation); returns null java


I really have no idea why this is happening. I am very new to android development so sorry if this a very nooby question. Basically bottomNavigationView is null after using the method "findViewById()". I know it's null because I've made it to be printed. I've search other posts and the conclusion I've made is that it's either the methods sequence or the xml file. But I can't seem to find any problem with them.

2022-05-18 17:05:02.236 6836-6836/com.example.smartchatters W/e.smartchatter: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2022-05-18 17:05:02.521 6836-6836/com.example.smartchatters I/System.out: bottomNavigationView ->null
2022-05-18 17:05:02.579 6836-6858/com.example.smartchatters D/HostConnection: HostConnection::get() New Host Connection established 0xf0751650, tid 6858

This is the only method in MainActivity.java:

import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView bottomNavigationView = findViewById(R.id.navigation);
        System.out.println("bottomNavigationView ->"+bottomNavigationView);

    }
}

navigation.xml file:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/navigation"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    app:startDestination="@id/mainFragment"
    >

    <fragment
        android:id="@+id/mainFragment"
        android:name="com.example.smartchatters.MainFragment"
        android:label="fragment_main"
        tools:layout="@layout/fragment_main" />
    <fragment
        android:id="@+id/contactsFragment"
        android:name="com.example.smartchatters.ContactsFragment"
        android:label="fragment_contacts"
        tools:layout="@layout/fragment_contacts" />
</navigation>

And gradle.build:

plugins {
    id 'com.android.application'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.example.smartchatters"
        minSdk 21
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.6.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    implementation 'androidx.navigation:navigation-fragment:2.4.2'
    implementation 'androidx.navigation:navigation-ui:2.4.2'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

Solution

  • You're setting as a layout activity_main.xml

    setContentView(R.layout.activity_main);

    And then method

    findViewById will search for view id inside of activity_main.xml file.

    So you need to use view id(that you use for BottomNavigationView) from your activity_main.xml

    If you don have a view with id "navigation" inside of activity_main.xml, the method will return null.