【问题标题】:Unable to start activity ComponentInfo: Error inflating class android.support.design.widget.CoordinatorLayout无法启动活动 ComponentInfo:膨胀类 android.support.design.widget.CoordinatorLayout 时出错
【发布时间】:2016-02-11 14:43:44
【问题描述】:

我遇到了这个错误,请你帮帮我。

Unable to start activity ComponentInfo
{com.kaniar.tappy_defender/com.kaniar.tappy_defender.MainActivity}: 
android.view.InflateException: 
Binary XML file line #2: 
Error inflating class android.support.design.widget.CoordinatorLayout

MainActivity.java

package com.kaniar.tappy_defender;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity implements View.OnClickListener {

// This is the entry point of the game
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Set the UI layout as the view
    setContentView(R.layout.activity_main);

    // Get a reference to the playButton in the layout
    final Button playButton = (Button)findViewById(R.id.playButton);

    // Listen for clicks
    playButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {
    // Must be the Play button
    // Create a new Intent object
    Intent intent = new Intent(this, GameActivity.class);

    // Start the GameActivity class via the Intent
    startActivity(intent);

    // Now shut this activity down
    finish();
}
}

XML 文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout   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:fitsSystemWindows="true"
tools:context="com.kaniar.tappy_defender.MainActivity"
android:background="@drawable/background">

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

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

LogCat 输出

02-11 18:24:07.980 21999-21999/com.kaniar.tappy_defender E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.kaniar.tappy_defender, PID: 21999
                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kaniar.tappy_defender/com.kaniar.tappy_defender.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.design.widget.CoordinatorLayout
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2377)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429)
                                                                           at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:110)
                                                                           at android.os.Looper.loop(Looper.java:193)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5333)
                                                                           at java.lang.reflect.Method.invokeNative(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:515)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
                                                                           at dalvik.system.NativeStart.main(Native Method)
                                                                        Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.design.widget.CoordinatorLayout
                                                                           at android.view.LayoutInflater.createView(LayoutInflater.java:620)
                                                                           at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
                                                                           at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:341)
                                                                           at android.app.Activity.setContentView(Activity.java:1948)
                                                                           at com.kaniar.tappy_defender.MainActivity.onCreate(MainActivity.java:17)
                                                                           at android.app.Activity.performCreate(Activity.java:5343)
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429) 
                                                                           at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                           at android.os.Looper.loop(Looper.java:193) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5333) 
                                                                           at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                           at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 
                                                                           at dalvik.system.NativeStart.main(Native Method) 
                                                                        Caused by: java.lang.reflect.InvocationTargetException
                                                                           at java.lang.reflect.Constructor.constructNative(Native Method)
                                                                           at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
                                                                           at android.view.LayoutInflater.createView(LayoutInflater.java:594)
                                                                           at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696) 
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:469) 
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
                                                                           at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:341) 
                                                                           at android.app.Activity.setContentView(Activity.java:1948) 
                                                                           at com.kaniar.tappy_defender.MainActivity.onCreate(MainActivity.java:17) 
                                                                           at android.app.Activity.performCreate(Activity.java:5343) 
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088) 
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331) 
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429) 
                                                                           at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                           at android.os.Looper.loop(Looper.java:193) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5333) 
                                                                           at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                           at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640) 
                                                                           at dalvik.system.NativeStart.main(Native Method) 
                                                                        Caused by: java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
                                                                           at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:34)
                                                                           at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:178)
                                                                           at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:172)
                                                                           at java.lang.reflect.Constructor.constructNative(Native Method) 
                                                                           at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
                                                                           at android.view.LayoutInflater.createView(LayoutInflater.java:594) 
                                                                           at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696) 
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:469) 
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
                                                                           at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
                                                                           at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:341) 
                                                                           at android.app.Activity.setContentView(Activity.java:1948) 
                                                                           at com.kaniar.tappy_defender.MainActivity.onCreate(MainActivity.java:17) 
                                                                           at android.app.Activity.performCreate(Activity.java:5343) 
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088) 
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331) 
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2429) 
                                                                           at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:110) 
                                                                           at android.os.Looper.loop(Looper.java:193) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5333) 
                                                                           at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                           at java.lang.reflect.Method.invoke(Method.java:515)

我真的不知道会发生什么,我可以构建项目,但是当我在手机或模拟器上运行应用程序时,我得到了这个! 如果有人知道解决方案,那就太棒了!

【问题讨论】:

  • 你必须扩展 AppCompatActivity 而不是 Activity
  • 不工作,我不明白为什么我必须这样做?

标签: java android xml android-studio


【解决方案1】:

尝试在您的 MainActivity 扩展中将 Activity 更改为 AppCompatAvtivity。

如果仍然不起作用,请将父主题更改为 AppTheme

<style name="Base.AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

【讨论】:

  • 在您的 style.xml 中,您应该拥有它,但可能使用不同的父级,您必须将其父级更改为 AppCompat 之一,就像我选择的那样
  • 你还需要这个依赖编译'com.android.support:design:22.2.0'和编译'com.android.support:appcompat-v7:23.1.1'(我假设你有它)
  • 我找到了解决方案!这是因为我删除了:AndroidManifest.xml 中的 android:theme="@style/Theme.AppCompat" 为什么我这样做?因为我也想把这一行: android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 但知道我的问题是如何将两者保存在同一个文件中?
  • 你必须在你的styles.xml中创建一个新的风格,像这样: 并在清单中添加这种新样式,而不是您拥有的样式
  • 如果您的问题得到解决,请将答案标记为正确
【解决方案2】:

我找到了解决方案,它是在你的 styles-v21.xml 中创建以下主题(如果你导入了示例文件,它应该已经存在,但如果它不存在,请创建它):

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style

> 并在清单中,在主要活动部分中调用它,如下所示(删除我之前突出显示的行并将其放在它的位置):

android:theme="@style/AppTheme.NoActionBar">

thios 应该可以解决问题。

InflateException Error inflating class android.support.design.widget.CoordinatorLayout

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    • 2017-02-20
    • 2021-06-05
    • 1970-01-01
    相关资源
    最近更新 更多