膨胀类android.support.design.widget.BottomNavigationView时出错

时间:2018-10-11 07:13:29

标签: java android xml

我有以下代码:

activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".Main2Activity">

    <android.support.design.widget.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        app:menu="@menu/nav_items" />

</RelativeLayout>

Main2Activity:

package asus.example.com.oop;

import android.os.Bundle; import android.app.Activity;

public class Main2Activity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

}

nav_items.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:icon="@drawable/home24"
        android:title="@string/home"
        />

    <item
        android:icon="@drawable/notifications24"
        android:title="@string/notifications"
        />

    <item
        android:icon="@drawable/person24"
        android:title="@string/account"
        />
</menu>

当我运行程序时,出现以下错误:

  

java.lang.RuntimeException:无法启动活动   ComponentInfo {asus.example.com.oop / asus.example.com.oop.Main2Activity}:   android.view.InflateException:二进制XML文件第9行:二进制XML   文件行9:错误夸大类   android.support.design.widget.BottomNavigationView造成原因:   android.view.InflateException:二进制XML文件第9行:二进制XML   文件行9:错误夸大类   android.support.design.widget.BottomNavigationView造成原因:   android.view.InflateException:二进制XML文件第9行:错误   膨胀类android.support.design.widget.BottomNavigationView   造成原因:java.lang.reflect.InvocationTargetException

我不知道,为什么我有这个问题。我添加了compile 'com.android.support:design'行以构建gradle,但仍然无法正常工作。怎么了?

1 个答案:

答案 0 :(得分:1)

要使用AppCompat功能,您的活动需要扩展AppCompatActivity

替换

public class Main2Activity extends Activity

public class Main2Activity extends AppCompatActivity