-1

Solution: I tried everything available on internet and nothing really worked for me. Then I found a solution. I didnt have the values-v23 folder in my project. If you don't have that, DO IT! and add this in style.xml of the Values- v23 folder

    <resources>

   <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
       <item name="windowActionBar">true</item>
        <item name="windowNoTitle">false</item>
    </style>
</resources>

I had splash screen and slider which was overlaying the action bar and android studi somehow got confused and my actionbar was returning null. After 2 weeks of research I found a tiny issue which no one seem to point out. I edited the line

        android:theme="@style/AppTheme"> 

with the actual theme i was using i.e

                           <android:theme="@style/Theme.AppCompat.Light.DarkActionBar"> 

It worked for me..

Question I had this app working totally fine until I made some changes.
I reverted back all of the changes but now I am getting NUllPointerException on ActionBar

 ActionBar ab = getSupportActionBar();

    if (ab != null) {
        ab.setLogo(R.mipmap.icon);
        ab.setDisplayUseLogoEnabled(true); //shows logo
        ab.setDisplayShowHomeEnabled(true); //shows home page
    }
    else {
        Toast.makeText(HomeScreen.this, "Feature not supported!", Toast.LENGTH_SHORT).show();

    }

I put the if else statements to check where the problem was. It runs the else part only. The piece of Home Screen code is this

package com.example.dell.optasia;

import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.media.MediaPlayer;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.support.v4.view.GestureDetectorCompat;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Locale;
import static android.view.GestureDetector.OnDoubleTapListener;
import static android.view.GestureDetector.OnGestureListener;
import static com.example.dell.optasia.R.id.editText;

public class HomeScreen extends AppCompatActivity implements
        OnGestureListener, OnDoubleTapListener {
    //used for text to speech
    TextToSpeech ttsobject;
    int result;
    TextView et;
    String text;
    String texttoplay;
    //uptil here
    private GestureDetectorCompat GestureDetect;
    private static TextView Viewtext;
    //used for speech to text
    public static final int REQUEST_OK = 1;
    public static TextView finalRes;
    public static ImageView imgBtn;
    //uptill here
    public MediaPlayer mp1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_screen);

    mp1 = MediaPlayer.create(HomeScreen.this, R.raw.startup);
    mp1.start();
    ActionBar ab = getSupportActionBar();

    if (ab != null) {
        ab.setLogo(R.mipmap.icon);
        ab.setDisplayUseLogoEnabled(true); //shows logo
        ab.setDisplayShowHomeEnabled(true); //shows home page
    }
    else {
        Toast.makeText(HomeScreen.this, "Feature not supported!", Toast.LENGTH_SHORT).show();

    }

    //for tap and hold and all other gestures
    Viewtext = (TextView) findViewById(R.id.textView);
    //onlongpress code needs this
    GestureDetect = new GestureDetectorCompat(this, this);
    GestureDetect.setOnDoubleTapListener(this);
    //text to speech code here
    et = (TextView) findViewById(editText);

    ttsobject = new TextToSpeech(HomeScreen.this, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int status) {
            if (status == TextToSpeech.SUCCESS) {
                result = ttsobject.setLanguage(Locale.UK);
            } else {
                Toast.makeText(HomeScreen.this, "Feature not supported!", Toast.LENGTH_SHORT).show();
            }
        }
    });

}

AndroidManifest.xml

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

package="com.example.dell.optasia">
<uses-sdk android:minSdkVersion="11" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/icon"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".Splashscreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".HomeScreen">
        <intent-filter>
            <action android:name="com.example.dell.optasia.HomeScreen" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity android:name=".AboutUs">
        <intent-filter>
            <action android:name="com.example.dell.optasia.AboutUs" />

            <category android:name="android.intent.category.About" />
        </intent-filter>
    </activity>
    <activity android:name=".Help"></activity>
</application>

Style.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
<item name="windowActionBar">true</item>

<item name="windowNoTitle">true</item>

</style>


<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />


<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>
Jiya Khan
  • 93
  • 9
  • You Actionbar returns null because you don't have actionbar but you are requesting using getActionbar() – Ahlem Jarrar Aug 19 '16 at 11:26
  • I am sorry I didn't understand your point. I had this same code working a few days back and the action bar does show on splash screen (Which shouldn't happen and wasn't happening a while ago I will look into that later) – Jiya Khan Aug 19 '16 at 11:40

1 Answers1

0
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
super.onCreate(savedInstanceState);
setContentView(R.layout...);

// experiment with the ActionBar 
ActionBar actionBar = getSupportActionBar();

}
Ahlem Jarrar
  • 1,109
  • 1
  • 12
  • 31