Android Studio:不幸的是该应用已停止。我在LayoutInflator.java

时间:2017-07-16 20:24:07

标签: android android-layout floating-action-button

根据评论,我在logcat下面添加了ActivityMain.xml文件。我正在尝试构建一个带有浮动操作栏的简单应用程序。我不知道我犯了哪些错误。

以下是logcat中的消息。

FATAL EXCEPTION: main
 Process: com.example.lenovo.makersbuilders5, PID: 3767
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lenovo.makersbuilders5/com.example.lenovo.makersbuilders5.MainActivity}: android.view.InflateException: Binary XML file line #15: Binary XML file line #15: Error inflating class android.support.design.widget.FloatingActionButton
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
     at android.app.ActivityThread.-wrap11(ActivityThread.java)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
     at android.os.Handler.dispatchMessage(Handler.java:102)
     at android.os.Looper.loop(Looper.java:148)
     at android.app.ActivityThread.main(ActivityThread.java:5443)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
  Caused by: android.view.InflateException: Binary XML file line #15: Binary XML file line #15: Error inflating class android.support.design.widget.FloatingActionButton
     at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
     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:292)
     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
     at com.example.lenovo.makersbuilders5.MainActivity.onCreate(MainActivity.java:18)
     at android.app.Activity.performCreate(Activity.java:6245)
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5443) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
  Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class android.support.design.widget.FloatingActionButton
     at android.view.LayoutInflater.createView(LayoutInflater.java:645)
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
     at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
     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:292) 
     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
     at com.example.lenovo.makersbuilders5.MainActivity.onCreate(MainActivity.java:18) 
     at android.app.Activity.performCreate(Activity.java:6245) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5443) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
  Caused by: java.lang.reflect.InvocationTargetException
     at java.lang.reflect.Constructor.newInstance(Native Method)
     at android.view.LayoutInflater.createView(LayoutInflater.java:619)
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) 
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
     at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
     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:292) 
     at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
     at com.example.lenovo.makersbuilders5.MainActivity.onCreate(MainActivity.java:18) 
     at android.app.Activity.performCreate(Activity.java:6245) 
     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130) 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490) 
     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354) 
     at android.os.Handler.dispatchMessage(Handler.java:102) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5443) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.graphics.drawable.Drawable$ConstantState.newDrawable()' on a null object reference
     at android.graphics.drawable.LayerDrawable$ChildDrawable.<init>(LayerDrawable.java:1723)
     at android.graphics.drawable.LayerDrawable$LayerState.<init>(LayerDrawable.java:1792)
     at android.graphics.drawable.RippleDrawable$RippleState.<init>(RippleDrawable.java:982)
     at android.graphics.drawable.RippleDrawable.createConstantState(RippleDrawable.java:973)
     at android.graphics.drawable.RippleDrawable.createConstantState(RippleDrawable.java:972)
     at android.graphics.drawable.LayerDrawable.mutate(LayerDrawable.java:1652)
     at android.graphics.drawable.RippleDrawable.mutate(RippleDrawable.java:959)
     at android.view.View.applyBackgroundTint(View.java:17429)
     at android.view.View.setBackgroundDrawable(View.java:17305)
     at android.support.design.widget.FloatingActionButton.access$001(FloatingActionButton.java:70)
     at android.support.design.widget.FloatingActionButton$ShadowDelegateImpl.setBackgroundDrawable(FloatingActionButton.java:831)
     at android.support.design.widget.FloatingActionButtonLollipop.setBackgroundDrawable(FloatingActionButtonLollipop.java:71)
     at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:181)
    at android.support.des

ActivityMain.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"
    tools:context="com.example.lenovo.makersbuilders5.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_youtube"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginBottom="260dp"
        android:layout_marginRight="@dimen/fab_margin_right"
        android:backgroundTint="@color/Color2"
        android:elevation="6dp"
        android:src="@drawable/ic_youtube"
        android:visibility="invisible"
        app:pressedTranslationZ="12dp" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_insta"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginBottom="180dp"
        android:layout_marginRight="@dimen/fab_margin_right"
        android:backgroundTint="@color/Color3"
        android:elevation="6dp"
        android:src="@drawable/ic_insta"
        android:visibility="invisible"
        app:pressedTranslationZ="12dp" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_fb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginBottom="100dp"
        android:layout_marginRight="@dimen/fab_margin_right"
        android:backgroundTint="@color/Color1"
        android:elevation="6dp"
        android:src="@drawable/ic_facebook"
        android:visibility="invisible"
        app:pressedTranslationZ="12dp" />


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_plus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_marginBottom="16dp"
        android:layout_marginRight="@dimen/fab_margin_right"
        android:elevation="6dp"
        android:src="@drawable/ic_plussign"
        app:pressedTranslationZ="12dp" />

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

LayoutInflator.java 只有错误的部分,因为有很多代码!!

import com.android.internal.R;

import android.annotation.LayoutRes;
import android.annotation.Nullable;

   protected LayoutInflater(LayoutInflater original, Context newContext) {
        mContext = newContext;
        mFactory = original.mFactory;
        mFactory2 = original.mFactory2;
        mPrivateFactory = original.mPrivateFactory;
        setFilter(original.mFilter);
    }

   public View inflate(@LayoutRes int resource, @Nullable ViewGroup root) {
        return inflate(resource, root, root != null);
    }

   public View inflate(XmlPullParser parser, @Nullable ViewGroup root) {
        return inflate(parser, root, root != null);
    }

   public View inflate(@LayoutRes int resource, @Nullable ViewGroup root, boolean attachToRoot) {
        final Resources res = getContext().getResources();
        if (DEBUG) {
            Log.d(TAG, "INFLATING from resource: \"" + res.getResourceName(resource) + "\" ("
                    + Integer.toHexString(resource) + ")");
        }

    public View inflate(XmlPullParser parser, @Nullable ViewGroup root, boolean attachToRoot) {
        synchronized (mConstructorArgs) {
            Trace.traceBegin(Trace.TRACE_TAG_VIEW, "inflate");


                Trace.traceEnd(Trace.TRACE_TAG_VIEW);
            }

2 个答案:

答案 0 :(得分:0)

android.view.InflateException: Binary XML file line #15: Error inflating class android.support.design.widget.FloatingActionButton

这是例外。我想也许你没有导入设计库;

compile 'com.android.support:design:25.4.0'

或者可能是不好的属性

编辑更新

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_youtube"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_marginBottom="260dp"
    android:layout_marginRight="@dimen/fab_margin_right"
    app:backgroundTint="@color/Color2"
    app:elevation="6dp"
    android:src="@drawable/ic_youtube"
    android:visibility="invisible"
    app:pressedTranslationZ="12dp" />

代码中的不同行

app:backgroundTint="@color/Color2"
    app:elevation="6dp"

答案 1 :(得分:0)

我想你忘了添加支持compile 'com.android.support:design:25.3.1' 只需将此依赖项添加到您的gradle文件中,就像这样

 dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:design:25.3.1'
    compile 'com.android.support:cardview-v7:25.3.1'
    compile 'com.android.support:support-v4:25.3.1'
}