bottomNavigationView = findViewById(r.id.navigation);返回null java

发布于 2025-01-30 07:17:32 字数 3309 浏览 5 评论 0原文

我真的不知道为什么会发生这种情况。我是Android开发的新手,很抱歉这是一个非常努力的问题。基本上,在使用方法“ FindViewById()”之后,底部范围为无效。我知道这是无效的,因为我已经打印了它。我搜索了其他帖子,我得出的结论是,它是方法序列或XML文件。但是我似乎找不到他们的任何问题。

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

这是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文件:

<?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>

和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'
}

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'
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

゛清羽墨安 2025-02-06 07:17:32

您将设置为布局Activity_main.xml

setContentView(r.layout.activity_main);

,然后方法

findViewById 将搜索activity_main.xml文件中的视图ID。

因此,您需要使用activity_main.xml的View ID(用于bottom navigationView),

如果您在activity_main.xml的内部有一个“ avarigation”的视图,则该方法将返回> null

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文