AppCompatActivity setContentView 上的 NullPointerException

NullPointerException on AppCompatActivity setContentView

我收到一个我真的不知道如何解释的错误。 我的应用程序在启动时崩溃并出现以下异常:

05-18 19:49:36.815    6795-6795/com.ispimi.ispimi E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.ispimi.ispimi, PID: 6795
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ispimi.ispimi/com.ispimi.ispimi.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        at android.app.ActivityThread.access0(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5254)
        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:903)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:715)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
        at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
        at com.ispimi.ispimi.MainActivity.onCreate(MainActivity.java:112)
        at android.app.Activity.performCreate(Activity.java:5990)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
            at android.app.ActivityThread.access0(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            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:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

所以空指针似乎是由第 112 行的 setContentView(R.layout.activity_main); 引发的,问题是布局存在,所以我不为什么找不到它(如果这确实是问题所在)。我也知道这作为一种表达方式有点被滥用,但之前工作得很好。

我正在使用 appcompat-v7:22.1.1 和 support-v4:22.1.1。

这是我的 onCreate 方法:

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mContext = getApplicationContext();

    Log.d(LOG_TAG, "onCreate");

    //I initialize the object that will be used to retrieve the location
    appLocationService = new AppLocationService(
            MainActivity.this);

    setContentView(R.layout.activity_main);
    ButterKnife.inject(this);

    dealsFragment = ((DealsFragment) getSupportFragmentManager()
            .findFragmentById(R.id.fragment_deals));
}

编辑:

activity_main.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/MainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:menu="main">

<fragment
    android:id="@+id/fragment_deals"
    class="com.ispimi.ispimi.DealsFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:layout="@android:layout/list_content"
    tools:menu="main" />

<view
    android:id="@+id/fab"
    android:layout_width="@dimen/fab_size"
    android:layout_height="@dimen/fab_size"
    android:layout_gravity="bottom|right"
    android:layout_marginBottom="16dp"
    android:color="@color/primary"
    android:indeterminate="false"
    android:max="100"
    android:src="@drawable/ic_fab_add"
    android:visibility="visible"
    fab:fbb_autoStart="true"
    fab:fbb_endBitmap="@drawable/ic_fab_complete"
    fab:fbb_hideProgressOnComplete="true"
    fab:fbb_progressColor="@color/primary_pressed"
    fab:fbb_progressWidthRatio="0.1"
    fab:fbb_showEndBitmap="true"
    android:layout_marginRight="16dp" />
 </FrameLayout>

在 super.onCreate 之后使用 setContentView,如下所示:

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mContext = getApplicationContext();

Log.d(LOG_TAG, "onCreate");

//I initialize the object that will be used to retrieve the location
appLocationService = new AppLocationService(
        MainActivity.this);


ButterKnife.inject(this);

dealsFragment = ((DealsFragment) getSupportFragmentManager()
        .findFragmentById(R.id.fragment_deals));

}

所以,我已经能够追踪到问题所在。 显然,我用于 FAB really 的自定义视图需要具有 class 属性:

class="mbanje.kurt.fabbutton.FabButton"

谢谢大家抽出时间。