AppCompatActivity setContentView上的NullPointerException

时间:2015-05-18 10:12:37

标签: java android nullpointerexception appcompat-v7-r22.1 appcompatactivity

我收到的错误是我真的不知道如何解释。 从以下例外开始我的应用程序崩溃:

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)

所以空指针似乎是由第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>

2 个答案:

答案 0 :(得分:0)

所以,我已经能够跟踪问题了。 显然,我用于FAB 真正的自定义视图需要具有属性:

class="mbanje.kurt.fabbutton.FabButton"

非常感谢您的时间。

答案 1 :(得分:-4)

在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));

}