如何在 Android Studio 中修复 "E/AndroidRuntime: FATAL EXCEPTION: main"?

How to fix "E/AndroidRuntime: FATAL EXCEPTION: main" in Android Studio?

已经编译好了,APK也安装好了。但是当我尝试在 Android phone 中打开它时,它一直强制关闭。

我试过几个设备,认为 phone android 版本是问题所在,但我认为这不是实际问题。

MainActivity.java

package com.example.androideatit;

import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    Button btnSignIn, btnSignUp;
    TextView txtSlogan;

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

        btnSignIn = (Button)findViewById(R.id.btnSignIn);
        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) {

            }
        });
    }
}


Main_Activity.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:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/my_bg"
    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/logo_eat_it"
        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>

Android logcat

2019-04-03 07:11:03.874 32076-32076/? I/Zygote: seccomp disabled by setenforce 0
2019-04-03 07:11:03.861 32076-32076/? I/main: type=1400 audit(0.0:26974): avc: denied { read } for name="u:object_r:bg_boot_complete_prop:s0" dev="tmpfs" ino=17954 scontext=u:r:untrusted_app:s0:c121,c256,c512,c768 tcontext=u:object_r:bg_boot_complete_prop:s0 tclass=file permissive=1
2019-04-03 07:11:03.861 32076-32076/? I/main: type=1400 audit(0.0:26975): avc: denied { read } for name="u:object_r:bg_daemon_prop:s0" dev="tmpfs" ino=17955 scontext=u:r:untrusted_app:s0:c121,c256,c512,c768 tcontext=u:object_r:bg_daemon_prop:s0 tclass=file permissive=1
2019-04-03 07:11:03.861 32076-32076/? I/main: type=1400 audit(0.0:26976): avc: denied { read } for name="u:object_r:bluetooth_a2dp_offload_prop:s0" dev="tmpfs" ino=17956 scontext=u:r:untrusted_app:s0:c121,c256,c512,c768 tcontext=u:object_r:bluetooth_a2dp_offload_prop:s0 tclass=file permissive=1
2019-04-03 07:11:03.861 32076-32076/? I/main: type=1400 audit(0.0:26977): avc: denied { read } for name="u:object_r:bluetooth_prop:s0" dev="tmpfs" ino=17957 scontext=u:r:untrusted_app:s0:c121,c256,c512,c768 tcontext=u:object_r:bluetooth_prop:s0 tclass=file permissive=1
2019-04-03 07:11:03.861 32076-32076/? I/main: type=1400 audit(0.0:26978): avc: denied { read } for name="u:object_r:bootloader_boot_reason_prop:s0" dev="tmpfs" ino=17958 scontext=u:r:untrusted_app:s0:c121,c256,c512,c768 tcontext=u:object_r:bootloader_boot_reason_prop:s0 tclass=file permissive=1
2019-04-03 07:11:03.881 32076-32076/? I/le.androideati: Late-enabling -Xcheck:jni
2019-04-03 07:11:03.959 32076-32083/? E/le.androideati: Failed to send DDMS packet REAQ to debugger (-1 of 20): Broken pipe
2019-04-03 07:11:04.260 32076-32076/? V/FA: Registered activity lifecycle callback
2019-04-03 07:11:04.274 32076-32076/? D/FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization.
2019-04-03 07:11:04.275 32076-32076/? D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
2019-04-03 07:11:04.275 32076-32076/? I/FirebaseInitProvider: FirebaseApp initialization successful
2019-04-03 07:11:04.321 32076-32098/? V/FA: Collection enabled
2019-04-03 07:11:04.321 32076-32098/? V/FA: App package, google app id: com.example.androideatit, 1:637601987854:android:d8941acf196c56e0
2019-04-03 07:11:04.322 32076-32098/? I/FA: App measurement is starting up, version: 15300
2019-04-03 07:11:04.322 32076-32098/? I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
2019-04-03 07:11:04.322 32076-32098/? I/FA: To enable faster debug mode event logging run:
      adb shell setprop debug.firebase.analytics.app com.example.androideatit
2019-04-03 07:11:04.322 32076-32098/? D/FA: Debug-level message logging enabled
2019-04-03 07:11:04.340 32076-32076/? V/FA: onActivityCreated
2019-04-03 07:11:04.358 32076-32098/? V/FA: Connecting to remote service
2019-04-03 07:11:04.376 32076-32076/? W/le.androideati: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
2019-04-03 07:11:04.377 32076-32076/? W/le.androideati: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
2019-04-03 07:11:04.394 32076-32098/? V/FA: Connection attempt already in progress
2019-04-03 07:11:04.439 32076-32076/? D/AndroidRuntime: Shutting down VM


    --------- beginning of crash
2019-04-03 07:11:04.443 32076-32076/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.androideatit, PID: 32076
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androideatit/com.example.androideatit.MainActivity}: android.view.InflateException: Binary XML file line #37: Binary XML file line #37: Error inflating class info.hoang8f.widget.FButton
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2951)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3086)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6718)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: android.view.InflateException: Binary XML file line #37: Binary XML file line #37: Error inflating class info.hoang8f.widget.FButton
     Caused by: android.view.InflateException: Binary XML file line #37: Error inflating class info.hoang8f.widget.FButton
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at android.view.LayoutInflater.createView(LayoutInflater.java:647)
        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.rInflate(LayoutInflater.java:866)
        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.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.example.androideatit.MainActivity.onCreate(MainActivity.java:18)
        at android.app.Activity.performCreate(Activity.java:7144)
        at android.app.Activity.performCreate(Activity.java:7135)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2931)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3086)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6718)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.UnsupportedOperationException: Can't convert value at index 1 to color: type=0x5
        at android.content.res.TypedArray.getColor(TypedArray.java:482)
        at info.hoang8f.widget.FButton.parseAttrs(FButton.java:116)
        at info.hoang8f.widget.FButton.<init>(FButton.java:52)
        at java.lang.reflect.Constructor.newInstance0(Native Method) 
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343) 
        at android.view.LayoutInflater.createView(LayoutInflater.java:647) 
        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.rInflate(LayoutInflater.java:866) 
        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.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) 
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
        at com.example.androideatit.MainActivity.onCreate(MainActivity.java:18) 
        at android.app.Activity.performCreate(Activity.java:7144) 
        at android.app.Activity.performCreate(Activity.java:7135) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1272) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2931) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3086) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1816) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6718) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:495) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

我希望它不会再强行关闭了。我目前正在关注 https://www.youtube.com/watch?v=Ad41Bh704ms&t=6s

中的视频教程

在您的布局中,您正在使用

<info.hoang8f.widget.FButton

但在您的 activity 中您使用的是 Button

也将 app 命名空间替换为 fbutton

    <info.hoang8f.widget.FButton
        xmlns:fbutton="http://schemas.android.com/apk/res-auto"
        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:buttonColor="@color/btnSignUp"
        fbutton:shadowColor="@android:color/black"
        fbutton:shadowEnabled="true"
        fbutton:shadowHeight="5dp"
        fbutton:cornerRadius="4dp"
        />

      <info.hoang8f.widget.FButton
        xmlns:fbutton="http://schemas.android.com/apk/res-auto"
        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:buttonColor="@color/btnSignActive"
        fbutton:shadowColor="@android:color/black"
        fbutton:shadowEnabled="true"
        fbutton:shadowHeight="5dp"
        fbutton:cornerRadius="4dp"
        />

主要活动

FButton btnSignIn = (FButton)findViewById(R.id.btnSignIn);
FButton btnSignUp = (FButton)findViewById(R.id.btnSignUp);