Will
Will

Reputation: 189

ActionBar giving nullPointerException?

I've been trying to figure this one out for a while. I have an app that uses the action bar but every time I want to call it in comes up null. Giving this error

    12-01 10:54:11.499: E/AndroidRuntime(2385): FATAL EXCEPTION: main
12-01 10:54:11.499: E/AndroidRuntime(2385): Process: com.example.jobpool, PID: 2385
12-01 10:54:11.499: E/AndroidRuntime(2385): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jobpool/com.example.jobpool.Signup}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.hide()' on a null object reference
12-01 10:54:11.499: E/AndroidRuntime(2385):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
12-01 10:54:11.499: E/AndroidRuntime(2385):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
12-01 10:54:11.499: E/AndroidRuntime(2385):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
12-01 10:54:11.499: E/AndroidRuntime(2385):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
12-01 10:54:11.499: E/AndroidRuntime(2385):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-01 10:54:11.499: E/AndroidRuntime(2385):     at android.os.Looper.loop(Looper.java:135)
12-01 10:54:11.499: E/AndroidRuntime(2385):     at android.app.ActivityThread.main(ActivityThread.java:5221)
12-01 10:54:11.499: E/AndroidRuntime(2385):     at java.lang.reflect.Method.invoke(Native Method)
12-01 10:54:11.499: E/AndroidRuntime(2385):     at java.lang.reflect.Method.invoke(Method.java:372)
12-01 10:54:11.499: E/AndroidRuntime(2385):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-01 10:54:11.499: E/AndroidRuntime(2385):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-01 10:54:11.499: E/AndroidRuntime(2385): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.hide()' on a null object reference
12-01 10:54:11.499: E/AndroidRuntime(2385):     at com.example.jobpool.Signup.onCreate(Signup.java:16)
12-01 10:54:11.499: E/AndroidRuntime(2385):     at android.app.Activity.performCreate(Activity.java:5933)
12-01 10:54:11.499: E/AndroidRuntime(2385):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
12-01 10:54:11.499: E/AndroidRuntime(2385):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
12-01 10:54:11.499: E/AndroidRuntime(2385):     ... 10 more

I know this has been discussed in other threads but I haven't found a solution that works for me. I have the app.support.v7 library added.

Here's the onCreate method:

public class Signup extends Activity 
{
      protected void onCreate(Bundle savedInstanceState) 
      {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.signup);
            getActionBar().hide();
            SetListeners();
        }

....

Here's the manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.jobpool"
    android:versionCode="1"
    android:versionName="1.0" >
    android:Debuggable = "True"
    android:VMsafemode ="True"
    android:AllowBackup = "True"
    android:AllowClearuserdate= "True"
    android:HardwareAccelerated = "True" 
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/logo_icon_large"
        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=".Signup"
            android:label="signup" >
        </activity>
        <activity
            android:name=".Dashboard"
            android:label="dashboard" >
        </activity>
        <activity
            android:name=".UserRegistration"
            android:label="@string/title_activity_user_registration" >
        </activity>
    </application>

</manifest>

and lastly my values/styles file

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
          <item name="android:windowNoTitle">true</item> <!-- Hides the Action Bar -->
          <item name="android:windowFullscreen">true</item> <!-- Hides the status bar -->
    </style>

</resources>

Upvotes: 1

Views: 4669

Answers (4)

Menjar Ali
Menjar Ali

Reputation: 41

if you are using support library put the code in this way

public class Signup extends Activity 
{
  protected void onCreate(Bundle savedInstanceState) 
  {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signup);
        getSupportActionBar().hide();
        SetListeners();
    }

....

Upvotes: -1

Martin
Martin

Reputation: 4846

you declare your AppTheme with the actionbar hidden: WIth these flags, it will never be there

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
      <item name="android:windowNoTitle">true</item> <!-- Hides the Action Bar -->
      <item name="android:windowFullscreen">true</item> <!-- Hides the status bar -->
</style>

Upvotes: 1

Sarthak Mittal
Sarthak Mittal

Reputation: 6124

Your getActionBar method is not returning anything i suppose, it is null, so it is unable to call the hide method.

By the way, how are you supposed to get ActionBar reference directly using getActionBar when you haven't even extended your Activity from ActionBarActivity???

Upvotes: 0

Blackbelt
Blackbelt

Reputation: 157487

if you are using the support library then you have to use getSupportActionBar() instead of getActionBar(), and instead of extends Activity you have to extends ActionBarActivity. Here you can find the documentation

Upvotes: 1

Related Questions