扩展类android.support.design.widget.AppBarLayout时出错

时间:2018-01-03 14:47:54

标签: android

我试图创建一个游戏节目的图片和角色名单吧。但是当我运行应用程序时,它会一直崩溃并显示错误:

  

错误类导出android.support.design.widget.AppBarLayout。

这是日志:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.user.celebguess, PID: 3166
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.celebguess/com.example.user.celebguess.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.design.widget.AppBarLayout
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                  at android.app.ActivityThread.-wrap11(Unknown Source:0)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                  at android.os.Handler.dispatchMessage(Handler.java:105)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6541)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
               Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.design.widget.AppBarLayout
               Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class android.support.design.widget.AppBarLayout
               Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.design.widget.AppBarLayout" on path: DexPathList[[zip file "/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/base.apk", zip file "/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.user.celebguess-t-FEyHBoSS08L3kZz9tsZg==/lib/x86, /system/lib, /vendor/lib]]
                  at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:93)
                  at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
                  at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                  at android.view.LayoutInflater.createView(LayoutInflater.java:606)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
                  at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
                  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                  at com.example.user.celebguess.MainActivity.onCreate(MainActivity.java:36)
                  at android.app.Activity.performCreate(Activity.java:6975)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
                  at android.app.ActivityThread.-wrap11(Unknown Source:0)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
                  at android.os.Handler.dispatchMessage(Handler.java:105)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6541)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

以下是activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.example.user.celebguess.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />
/>

此处还有styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

此处还有Java部分的main_activity.java

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

private GridView gridView;
private SharedPreferences preferences;
private ArrayAdapter<String> names;
private Toast currentToast;
private ImageView img;
private int numberOfChoice;
private ArrayList<String> nameList;
private ArrayList<Integer> photos;
private ArrayList<String> nameForChoose;
private Map<String,Integer> nameMap;
private int currentPicNumber;

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

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

}

@Override
protected void onStart() {
    super.onStart();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    int orientation = getResources().getConfiguration().orientation;
    if(orientation== Configuration.ORIENTATION_PORTRAIT) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
    }
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();


    if (id == R.id.action_settings) {
        Intent intent = new Intent(this,Settings.class);
        startActivity(intent);
        return true;
    }

    return super.onOptionsItemSelected(item);
}

3 个答案:

答案 0 :(得分:0)

please add this line

##设计库,您可以在此处复制并添加到build.gradle文件

  • 实施'com.android.support:design:28.0.0'

答案 1 :(得分:0)

将以下内容添加到bulid.gradle:app模块中:

implementation 'com.android.support:design:28.0.0'

答案 2 :(得分:0)

对于最新版本的android,请在 build.gradle(app)

implementation 'com.android.support:design:30.0.0'

,并在 .xml 中使用:

<com.google.android.material.appbar.AppBarLayout