NPE错误膨胀类片段:NavigationDrawerFragment

时间:2014-11-24 21:58:09

标签: android android-layout android-activity android-fragments android-actionbar

当我在Android Studio中创建新的导航抽屉活动时,基本示例运行完美。我已经更改了几行来集成Material Design和v4-v7支持,因此应用程序中断了。我还在Firebase中添加了一些基本代码,这些代码使用Google +进行简单的身份验证。

启动应用程序将返回运行时异常和“错误膨胀类片段”消息。

我猜测NPE来自不被夸大的观点,但我不知道这是怎么回事。

下面是我的logcat,我的活动和布局。任何帮助或指导将不胜感激。

logcat的:

11-24 16:29:26.735    6127-6127/com.starkenglish E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.starkenglish, PID: 6127
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.starkenglish/com.starkenglish.MainActivity}: android.view.InflateException: Binary XML file line #19: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5146)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class fragment
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:343)
            at android.app.Activity.setContentView(Activity.java:1929)
            at com.starkenglish.MainActivity.onCreate(MainActivity.java:110)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5146)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.starkenglish.NavigationDrawerFragment.onCreateView(NavigationDrawerFragment.java:101)
            at android.app.Fragment.performCreateView(Fragment.java:1700)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:866)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040)
            at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142)
            at android.app.Activity.onCreateView(Activity.java:4786)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:343)
            at android.app.Activity.setContentView(Activity.java:1929)
            at com.starkenglish.MainActivity.onCreate(MainActivity.java:110)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5146)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
            at dalvik.system.NativeStart.main(Native Method)

为了保持这篇文章的清洁,我在github上发布了我的代码作为GIST。

如果缺少代码/文件,请告诉我,我会立即将其包含在内。

1 个答案:

答案 0 :(得分:1)

您的主要活动扩展了FragmentActivity,以及获取actionBar

列在您的NavigationDrawerFragment line 101中,就像错误中所说的那样

   mDrawerListView.setAdapter(new ArrayAdapter<String>(
                getActivity().getActionBar().getThemedContext(), <== this is the null part
                android.R.layout.simple_list_item_activated_1,
                android.R.id.text1,
                new String[]{
                        getString(R.string.title_section1),
                        getString(R.string.title_section2),
                        getString(R.string.title_section3),
                }));

您需要从app compat

扩展ActionBarActivity

引自文档

  

ActionBarCompat包含一个Activity类,其中包含所有Activity   类应该扩展:ActionBarActivity。这个类本身也在扩展   从FragmentActivity,你可以继续使用你的碎片   应用