二进制XML文件行#38:错误导致类info.hoang8f.widget.FButton

时间:2018-04-04 13:35:31

标签: android

嘿,我刚刚开始学习android studio。然后我通过在youtube上观看一些教程并学习它来学习。但是当我想测试或启动项目时,我得到了一些错误消息二进制XML文件行#38:错误导致类info.hoang8f.widget.FButton等。

04-04 20:00:18.896 3492-3492/com.example.exochese.project1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.exochese.project1, PID: 3492
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.exochese.project1/com.example.exochese.project1.MainActivity}: android.view.InflateException: Binary XML file line #38: Error inflating class info.hoang8f.widget.FButton
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2330)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
    at android.app.ActivityThread.access$800(ActivityThread.java:154)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5273)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
 Caused by: android.view.InflateException: Binary XML file line #38: Error inflating class info.hoang8f.widget.FButton
    at android.view.LayoutInflater.createView(LayoutInflater.java:637)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:747)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:810)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:508)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:418)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378)
    at android.app.Activity.setContentView(Activity.java:2158)
    at com.example.exochese.project1.MainActivity.onCreate(MainActivity.java:17)
    at android.app.Activity.performCreate(Activity.java:6041)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1109)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2283)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) 
    at android.app.ActivityThread.access$800(ActivityThread.java:154) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5273) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703) 
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.newInstance(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
    at android.view.LayoutInflater.createView(LayoutInflater.java:611)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:747) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:810) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:813) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:508) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:418) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378) 
    at android.app.Activity.setContentView(Activity.java:2158) 
    at com.example.exochese.project1.MainActivity.onCreate(MainActivity.java:17) 
    at android.app.Activity.performCreate(Activity.java:6041) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1109) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2283) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) 
    at android.app.ActivityThread.access$800(ActivityThread.java:154) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5273) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703) 
 Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x5
    at android.content.res.TypedArray.getColor(TypedArray.java:406)
    at info.hoang8f.widget.FButton.parseAttrs(FButton.java:116)
    at info.hoang8f.widget.FButton.<init>(FButton.java:52)
    at java.lang.reflect.Constructor.newInstance(Native Method) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:288) 
    at android.view.LayoutInflater.createView(LayoutInflater.java:611) 
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:747) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:810) 
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:813) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:508) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:418) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:365) 
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:378) 
    at android.app.Activity.setContentView(Activity.java:2158) 
    at com.example.exochese.project1.MainActivity.onCreate(MainActivity.java:17) 
    at android.app.Activity.performCreate(Activity.java:6041) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1109) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2283) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392) 
    at android.app.ActivityThread.access$800(ActivityThread.java:154) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5273) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)

=============================================== ===========================

Main

&#13;
&#13;
package com.example.exochese.project1;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    Button btnSignIn,btnSignUp;
    TextView txtSlogan;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnSignIn =(Button) findViewById(R.id.btnSignUp);
        btnSignUp =(Button)findViewById(R.id.btnSignUp);
        txtSlogan = (TextView)findViewById(R.id.txtSlogan);
        Typeface face = Typeface.createFromAsset(getAssets(),"fonts/NABILA.TTF");
        txtSlogan.setTypeface(face);

        btnSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        btnSignUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }
}
&#13;
&#13;
&#13;

Gradle

&#13;
&#13;
apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.example.exochese.project1"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    //add library
    compile 'com.google.firebase:firebase-core:12.0.0'
    compile 'com.google.firebase:firebase-database:12.0.0'
    compile 'info.hoang8f:fbutton:1.0.5'
    compile 'com.rengwuxian.materialedittext:library:2.1.4'

}
apply plugin: 'com.google.gms.google-services'
&#13;
&#13;
&#13;

Activity

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@drawable/bg_1_my"
    tools:context=".MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    <ImageView
        android:src="@drawable/mangan"
        android:layout_width="200dp"
        android:layout_height="200dp" />
    <TextView
        android:gravity="center"
        android:id="@+id/txtSlogan"
        android:text="@string/slogan"
        android:textColor="@android:color/white"
        android:layout_width="200dp"
        android:layout_height="wrap_content" />


    </LinearLayout>
    
    <LinearLayout
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:weightSum="2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <info.hoang8f.widget.FButton
            android:id="@+id/btnSignUp"
            android:text="Sign Up"
            android:textColor="@android:color/white"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            app:buttonColor="@color/btnSignUp"
            app:shadowColor="@android:color/black"
            app:shadowEnabled="true"
            app:shadowHeight="5dp"
            app:cornerRadius="4dp"
            />

        <info.hoang8f.widget.FButton
            android:id="@+id/btnSignIn"
            android:text="Sign In"
            android:textColor="@android:color/white"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            app:buttonColor="@color/btnSignActive"
            app:shadowColor="@android:color/black"
            app:shadowEnabled="true"
            app:shadowHeight="5dp"
            app:cornerRadius="4dp"
            />
    </LinearLayout>
</RelativeLayout>
&#13;
&#13;
&#13;

当我想运行应用程序时。应用程序突然关闭,没有任何警告。 (我直接通过Android智能手机运行它)。 这是main和app的代码

这是我使用的颜色格式

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#004d40</color>
    <color name="colorPrimaryDark">#00251a</color>
    <color name="colorAccent">#39796b</color>

    <color name="btnSignUp">#39796b</color>
    <color name="btnSignActive">#A9d440</color>
</resources>

有什么问题?

2 个答案:

答案 0 :(得分:1)

执行以下操作

第1步:

在上面的build.gradle(Module:app)中 取代

编译'info.hoang8f:fbutton:1.0.5'

编译'com.github.jd-alexander:android-flat-button:v1.1'

或更好

实施'com.github.jd-alexander:android-flat-button:v1.1'

避免构建警告。

关于libray的详细信息可以是foud here

第2步:

在Build.gradle(项目)文件中

添加

maven {url“https://jitpack.io”}

在您的存储库中

您的存储库结构应该如下所示

allprojects {
repositories {
    google()
    jcenter()
    maven { url "https://jitpack.io" }
}

}

在您的activity.xml文件中

添加

xmlns:fbutton="http://schemas.android.com/apk/res-auto"

在相对布局声明之后,然后使用 fbutton 属性而不是 app

总之,您的 buid.gradle(模块)应如下所示

apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.example.exochese.project1"
        minSdkVersion 21
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
    //add library
    compile 'com.google.firebase:firebase-core:12.0.0'
    compile 'com.google.firebase:firebase-database:12.0.0'
    compile 'com.github.jd-alexander:android-flat-button:v1.1'
    compile 'com.rengwuxian.materialedittext:library:2.1.4'

}
apply plugin: 'com.google.gms.google-services'

您的activity_main.xml应如下所示

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:fbutton="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:background="@drawable/bg_1_my"
    tools:context=".MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    <ImageView
        android:src="@drawable/mangan"
        android:layout_width="200dp"
        android:layout_height="200dp" />
    <TextView
        android:gravity="center"
        android:id="@+id/txtSlogan"
        android:text="@string/slogan"
        android:textColor="@android:color/white"
        android:layout_width="200dp"
        android:layout_height="wrap_content" />


    </LinearLayout>
    
    <LinearLayout
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        android:weightSum="2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <info.hoang8f.widget.FButton
            android:id="@+id/btnSignUp"
            android:text="Sign Up"
            android:textColor="@android:color/white"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            fbutton:fButtonColor="@color/btnSignUp"
            fbutton:shadowColor="@android:color/black"
            fbutton:shadowEnabled="true"
            fbutton:shadowHeight="5dp"
            fbutton:cornerRadius="4dp"
            />

        <info.hoang8f.widget.FButton
            android:id="@+id/btnSignIn"
            android:text="Sign In"
            android:textColor="@android:color/white"
            android:layout_weight="1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="8dp"
            fbutton:fButtonColor="@color/btnSignActive"
            fbutton:shadowColor="@android:color/black"
            fbutton:shadowEnabled="true"
            fbutton:shadowHeight="5dp"
            fbutton:cornerRadius="4dp"
            />
    </LinearLayout>
</RelativeLayout>

答案 1 :(得分:0)

该错误告诉您布局无法膨胀,因为您的FButton实现存在错误:

Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x5
    at android.content.res.TypedArray.getColor(TypedArray.java:406)
    at info.hoang8f.widget.FButton.parseAttrs(FButton.java:116)

您没有显示FButton类,但在初始化时似乎是一个问题,同时读取您添加到布局中的属性。其中之一:

        app:buttonColor="@color/btnSignUp"
        app:shadowColor="@android:color/black"
        app:shadowEnabled="true"
        app:shadowHeight="5dp"
        app:cornerRadius="4dp"

我说问题是你在“btSignUp”字段中放置的值,它不能转换为颜色(另一种颜色来自Android,所以它肯定没问题)。 ¿你使用的颜色格式是“#xxxxxx”吗?在适当的标签上?别忘了#。像这样的东西(colors.xml):

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="btnSignUp">#3F51B5</color>
</resources>

问候。