2

I tried to implement an Setting Activity class but it give's me back an error:

Caused by: java.lang.NullPointerException
            at com.example.andrei.sunshine.SettingsActivity.setupActionBar(SettingsActivity.java:58)
            at com.example.andrei.sunshine.SettingsActivity.onCreate(SettingsActivity.java:48)
            at android.app.Activity.performCreate(Activity.java:5264)

This is the line : getActionBar().setDisplayHomeAsUpEnabled(true);

And the AndroidManifest is :

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.INTERNET" />

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".DetailActivity"
        android:label="@string/title_activity_detail"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.andrei.sunshine.MainActivity" />
    </activity>
    <activity
        android:name=".SettingsActivity"
        android:label="@string/title_activity_settings"
        android:parentActivityName=".MainActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.andrei.sunshine.MainActivity" />
    </activity>
</application>

Please help me

Darshan Lila
  • 5,772
  • 2
  • 24
  • 34

4 Answers4

2

If your SettingsActivity extend ActionBarActivity, your getActionBar(); will be available.

public class SettingsActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yourLayout);
        //If you use support library
        //getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

See the documentation for more info.

Marcus
  • 6,697
  • 11
  • 46
  • 89
0

You should check this reference http://developer.android.com/intl/es/guide/topics/ui/actionbar.html

If you are supporting API levels lower than 11:

import android.support.v7.app.ActionBar

If supporting only API level 11 and higher:

import android.app.ActionBar

Instead of using getActionBar, use getSupportActionBar

Ilenca
  • 301
  • 2
  • 10
0

It can happen if your theme is NoTitleBar or NoActionBar, or if you call getActionBar() before setContentView(R.layout.layout);

nbaroz
  • 1,795
  • 13
  • 14
0

Try to change it getSupportActionBar() if you are using appcompact ActionbarActivity

Jitty Aandyan
  • 1,994
  • 1
  • 13
  • 12