bottomNavigationView = findViewById(r.id.navigation);返回null java
我真的不知道为什么会发生这种情况。我是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将设置为布局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.xmlIf you don have a view with id "navigation" inside of activity_main.xml, the method will return
null
.