Search code examples
javaandroidnullpointerexceptionandroid-appcompatappcompatactivity

NullPointerException on AppCompatActivity setContentView


I'm getting an error that I don't really know how to explain. My app crashes when starting with the following exception:

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.access$800(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.access$800(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)

So the null pointer seems to be raised by the setContentView(R.layout.activity_main); on line 112, the problem is that the layout is present so I don't why is not able to find it (if that's is really the issue). Also I know that is a little bit abused as an expression, but is working perfectly before.

I'm using appcompat-v7:22.1.1 and support-v4:22.1.1.

This is my onCreate method:

@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));
}

EDIT:

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>

Solution

  • So, I have been able to track the problem. Apparently the custom view that I use for the FAB really needs to have the class attribute:

    class="mbanje.kurt.fabbutton.FabButton"
    

    Thanks anyway for your time guys.