我收到一个我真的不知道如何解释的错误。我的应用程序在出现以下异常时崩溃:
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)
所以空指针似乎是由setContentView(R.layout.activity_main); 在第 112 行,问题在于布局存在,所以我不知道为什么找不到它(如果这确实是问题所在)。我也知道这作为一种表达方式有点被滥用,但之前工作得很好。
我正在使用 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>