getsupportActionBar()。setDisplayHomeAsUpEnabled(true).setDisplayHomeAsUpEnabled(true)抛出NullPointerException

时间:2015-06-08 13:41:24

标签: java android nullpointerexception android-actionbar

我的应用程序每次都崩溃,错误为java.lang.RuntimeException: Unable to start activity ComponentInfo{sidssol.com.bit_taxi_b2c/sidssol.com.bit_taxi_b2c.Home}: java.lang.NullPointerException,因为getActionBar()。setDisplayHomeAsUpEnabled(true),粘贴了所有必需的代码。任何人都可以查看此代码

public class Home extends ActionBarActivity {
       adapter = new NavDrawerListAdapter(getApplicationContext(),
                        navDrawerItems);


                mDrawerList.setAdapter(adapter);

                // enabling action bar app icon and behaving it as toggle button
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                getSupportActionBar().setHomeButtonEnabled(true);

                mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                        R.drawable.ic_drawer, //nav menu toggle icon
                        R.string.app_name, // nav drawer open - description for accessibility
                        R.string.app_name // nav drawer close - description for accessibility
                )
}

Style.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar" >
        <item name="android:windowTranslucentStatus">true</item>
    </style>


    <style name="Theme.Test" parent="@style/Theme.AppCompat.Light">

        <!-- customize the color palette -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="drawerArrowStyle">@style/Theme.Test.DrawerArrowStyle</item>
    </style>

    <style name="Theme.Test.DrawerArrowStyle" parent="@style/Widget.AppCompat.DrawerArrowToggle">
        <item name="spinBars">true</item>
        <item name="color">@android:color/white</item>
    </style>
    <style name="Divider">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">1dp</item>
        <item name="android:background">?android:attr/listDivider</item>
    </style>

</resources>

Androidmainfest.xml

  <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".loginForm"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>       

3 个答案:

答案 0 :(得分:1)

正如您所看到的,您的主题表明没有操作栏

Theme.AppCompat.Light.NoActionBar

如果您使用该主题,则需要使用Toolbar或更改主题以使用操作栏

答案 1 :(得分:0)

更改这些行

  <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar" >
        <item name="android:windowTranslucentStatus">true</item>
    </style>

跟随

  <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light" >
        <item name="android:windowTranslucentStatus">true</item>
    </style>

或使用android:theme="@style/AppTheme"

更改android:theme="@style/Theme.Test"

答案 2 :(得分:0)

此外,不推荐使用ActionBarActivity,您应该使用AppCompatActivity。