我是Android dev的新手,为什么我会遇到这个奇怪的错误?

时间:2016-07-14 14:50:33

标签: android

这是我的主要活动java文件:

package com.example.fg;

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

public class MainActivity extends Activity {

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

    }

}

这就是它的布局

<?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.example.fg.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" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />

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

这是我得到的错误:

FATAL EXCEPTION: main
     Process: com.example.fg, PID: 6946
     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fg/com.example.fg.MainActivity}: android.view.InflateException: Binary XML file line #15: Error inflating class android.support.v7.widget.Toolbar
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
     at android.app.ActivityThread.access$900(ActivityThread.java:166)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:146)
     at android.app.ActivityThread.main(ActivityThread.java:5511)
     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:1283)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
     at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class android.support.v7.widget.Toolbar
     at android.view.LayoutInflater.createView(LayoutInflater.java:626)
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
     at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
     at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:366)
     at android.app.Activity.setContentView(Activity.java:2009)
     at com.example.fg.MainActivity.onCreate(MainActivity.java:11)
     at android.app.Activity.performCreate(Activity.java:5322)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2303)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391) 
     at android.app.ActivityThread.access$900(ActivityThread.java:166) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:146) 
     at android.app.ActivityThread.main(ActivityThread.java:5511) 
     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:1283) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
     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:600)
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702) 
     at android.view.LayoutInflater.rInflate(LayoutInflater.java:761) 
     at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:498) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:354) 
     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:366) 
     at android.app.Activity.setContentView(Activity.java:2009) 
     at com.example.fg.MainActivity.onCreate(MainActivity.java:11) 
     at android.app.Activity.performCreate(Activity.java:5322) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2303) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391) 
     at android.app.ActivityThread.access$900(ActivityThread.java:166) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:146) 
     at android.app.ActivityThread.main(ActivityThread.java:5511) 
     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:1283) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
     at dalvik.system.NativeStart.main(Native Method) 
     Caused by: java.lang.IllegalStateException: This app has been built with an incorrect configuration. Please configure your build for VectorDrawableCompat.
     at android.support.v7.widget.AppCompatDrawableManager.checkVectorDrawableSetup(AppCompatDrawableManager.java:692)
     at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:186)
     at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:181)
     at android.support.v7.widget.TintTypedArray.getDrawable(TintTypedArray.java:67)
     at android.support.v7.widget.Toolbar.<init>(Toolbar.java:298)
     at android.support.v7.widget.Toolbar.<init>(Toolbar.java:229)
     at java.lang.reflect.Constructor.constructNative(Native Method) 
     at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
     at android.view.LayoutInflater.createView(LayoutInflater.java:600) 
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702) 
     at android.view.LayoutInflater.rInflate(LayoutInflater.java:761) 
     at android.view.LayoutInflater.rInflate(LayoutInflater.java:769) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:498) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:398) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:354) 
     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:366) 
     at android.app.Activity.setContentView(Activity.java:2009) 
     at com.example.fg.MainActivity.onCreate(MainActivity.java:11) 
     at android.app.Activity.performCreate(Activity.java:5322) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2303) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391) 
     at android.app.ActivityThread.access$900(ActivityThread.java:166) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:146) 
     at android.app.ActivityThread.main(ActivityThread.java:5511) 
     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:1283) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099) 
     at dalvik.system.NativeStart.main(Native Method) 

每当我尝试创建一个新的空白应用程序时,我都会遇到此问题,请帮忙 7天前回来的一切都很顺利,但现在我从假期回来后什么都不想工作。

1 个答案:

答案 0 :(得分:0)

您的活动应该扩展AppCompatActivity:

public class MyActivity extends AppCompatActivity {
  // ...
}

XML:

    <android.support.v7.widget.Toolbar
   android:id="@+id/my_toolbar"
   android:layout_width="match_parent"
   android:layout_height="?attr/actionBarSize"
   android:background="?attr/colorPrimary"
   android:elevation="4dp"
   android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
   app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

然后:

@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_my);
            Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
            setSupportActionBar(myToolbar);
            }

参考:https://developer.android.com/training/appbar/setting-up.html