简体   繁体   中英

getsupportActionBar().setDisplayHomeAsUpEnabled(true).setDisplayHomeAsUpEnabled(true) throws NullPointerException

My app crashes each time with error as java.lang.RuntimeException: Unable to start activity ComponentInfo{sidssol.com.bit_taxi_b2c/sidssol.com.bit_taxi_b2c.Home}: java.lang.NullPointerException for getActionBar().setDisplayHomeAsUpEnabled(true),pasted all the required code.Can anyone check this code

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>       

As you can see your theme states there is no actionbar

Theme.AppCompat.Light.NoActionBar

if you are using that theme you will need to use the Toolbar or change your theme to use the actionbar

Change these lines

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

with follow

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

or change android:theme="@style/AppTheme" with android:theme="@style/Theme.Test"

另外,不建议使用ActionBarActivity,应使用AppCompatActivity。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM