0

大约 2 年前在这里提出了一个非常相似的问题,但尝试该解决方案并没有帮助。对我来说,设置内容视图时会引发错误:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.name.app, PID: 7732
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.name.app/com.name.app.MainPageActivity}: android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class android.support.design.internal.BottomNavigationView
...
        at com.name.app.MainPageActivity.onCreate(MainPageActivity.java:21)
...

我只想突出显示在我的代码中抛出错误的部分。现在这里是 Java 类(注意第 21 行是 setContentView(R.layout.activity_mainpage);):

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

import androidx.annotation.NonNull;

import com.google.android.material.bottomnavigation.BottomNavigationView;


public class MainPageActivity extends Activity {

    private BottomNavigationView bottomNavView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainpage);

        bottomNavView = findViewById(R.id.bottomNavigation);
        bottomNavView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch(item.getItemId()) {
                    case R.id.action_skills:
                        System.out.println("selected Skills in setOnNavigationItemSelectedListener");
                        break;
                    case R.id.action_notes:
                        System.out.println("selected Notes in setOnNavigationItemSelectedListener");
                        break;
                    case R.id.action_preflight:
                        System.out.println("selected Preflight in setOnNavigationItemSelectedListener");
                        break;
                    case R.id.action_myclass:
                        System.out.println("selected My Class in setOnNavigationItemSelectedListener");
                        break;
                }
                return true;
            }
        });
    }
}

以下是关于底部导航视图的两个相关 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainPageActivity"
    android:background="@drawable/background"
    android:id="@+id/activity_mainpage">

        <android.support.design.internal.BottomNavigationView
            android:id="@+id/bottomNavigation"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_alignParentBottom="true"
            app:menu="@menu/student_bottom_nav_menu"
            app:itemIconTint="@android:color/white"
            app:itemTextColor="@android:color/white" />

</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_skills"
        android:enabled="true"
        android:title="Skills"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/action_notes"
        android:enabled="true"
        android:title="Notes"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/action_preflight"
        android:enabled="true"
        android:title="Preflight"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/action_myclass"
        android:enabled="true"
        android:title="My Class"
        app:showAsAction="ifRoom" />
</menu>

和我的应用程序 gradle 文件:

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.name.app"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
    implementation 'com.google.firebase:firebase-auth:19.2.0'
    implementation 'com.google.firebase:firebase-firestore:21.4.0'
    implementation 'androidx.recyclerview:recyclerview:1.1.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'com.android.support:design:28.0.0'
}

这个错误对我没有任何意义,非常感谢任何帮助。谢谢。

4

1 回答 1

0

我很笨...我解决了我自己的问题。问题是

 <android.support.design.internal.BottomNavigationView

在主 xml 文件中。我把它改成

<com.google.android.material.bottomnavigation.BottomNavigationView

现在它可以工作了。

于 2020-02-23T18:06:25.947 回答