0

My app uses the android them Holo.NoActionBar theme to hide the action bar and display the activity .

This works fine on emulator and my Nexus 4 ,device actionbar is hidden.

however when it is run on HTC one , it ignores the android theme settings and displays the action bar. It might also be displayed on some other samsung or sony devices which have custom OEM skins. I have made sure there are no references to action bar or menu options in activity java file or android manifest file.

So i googled how to hide action bar and tried this How to hide action bar before activity is created, and then show it again?

and wrote the code

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
getActionBar().hide();
setContentView(R.layout.activity_my);

and http://developer.android.com/guide/topics/ui/actionbar.html

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar actionBar = getSupportActionBar();
actionBar.hide();
setContentView(R.layout.activity_my);

However in both cases the IDE displays a warning saying the Method invocation'actionBar.hide()' may produce 'java.lang.NullPointerException'

"This inspection analyzes method control and data flow to report possible conditions that are always true or false, expressions whose value is statically proven to be constant and situations that can lead to nullability contract violations"

Although I am new to android development I know that NullPointerException should not be ignored.

But in this activity , it is important to hide action bar.

How do i modify this code so that it is not error prone??

UPDATE: and why is Holo.NoActionBar not working with HTC one . It is still displaying the action bar, while installing the same apk does not display action bar on nexus 4

Android manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.xxxxxxxx.xxxxxxxxx" >

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

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    .
    .
    lots of non-relevant activities
    .
    .

    <activity
        android:name=".Setting"
        android:label="@string/title_activity_setting"
        android:parentActivityName=".MyActivity"
        android:screenOrientation="portrait">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.xxxxxxxxx.xxxxxxxxxx.MyActivity" />
    </activity>
    <activity
        android:name=".t1"
        android:screenOrientation="portrait"
    >
    </activity>
    <activity
        android:name=".t2"
        android:screenOrientation="portrait"
    >
    </activity>
    <activity
        android:name=".t3"
        android:screenOrientation="portrait"
        >
    </activity>
    <activity
        android:name=".t4"
        android:screenOrientation="portrait"
         >
    </activity>
    <activity
        android:name=".t5"
        android:label="@string/title_activity_t5"
        android:screenOrientation="portrait"
        android:parentActivityName=".MyActivity" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.xxxxxxxxxx.xxxxxxxxxx.MyActivity" />
    </activity>
</application>

</manifest>

t1.java, without Actionbar.hide()

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;


public class t1 extends Activity {

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

    RelativeLayout rl = (RelativeLayout) findViewById(R.id.rel1);
    rl.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent newactivity= new Intent(getApplicationContext(), t2.class);
            startActivity(newactivity);
        }
    });
}


}

and t2,t3,t4,t5 have similar code

layout file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.xxxxxxxxxx.xxxxxxxx.t1"
android:background="@drawable/finalp2"
android:id="@+id/rel1">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:layout_marginTop="40dp"
    android:background="@drawable/bat"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="40dp" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageViewbottom"
    android:background="@drawable/bat"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginRight="40dp"
    android:layout_marginBottom="42dp" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView2"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:background="@android:drawable/ic_media_pause" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView4"
    android:layout_below="@+id/imageView"
    android:layout_toRightOf="@+id/imageView"
    android:layout_toEndOf="@+id/imageView"
    android:layout_marginTop="112dp"
    android:background="@drawable/ball1" />


</RelativeLayout>

How i selected the theme : http://s16.postimg.org/6vuyjhwth/noactionbar.png

I have not created any other style.xml file, did i miss something?? If i did miss something how is it working fine on nexus 4 and why is it showing without action bar in IDE?

My app use SDK version 15 and higher

Community
  • 1
  • 1
Akshit Rewari
  • 941
  • 2
  • 13
  • 26
  • You should probably first ask a question to figure out why your theme is not working as it certainly should (and does for many other developers). – ianhanniballake Aug 17 '14 at 22:19
  • @ianhanniballake: the theme is working on nexus devices and emulator, Problem I have faced is on HTC one probably due to some custom skins. – Akshit Rewari Aug 17 '14 at 22:26
  • 1
    Yes, that's what you said. You should solve that problem first rather than trying to band-aid over it. Obviously it is working for thousands of other developers so we'd love to figure out what is wrong for your case. – ianhanniballake Aug 17 '14 at 22:28
  • You'll want to include a bunch of actual code you've written before we can figure out what is wrong - particularly your AndroidManifest.xml, your styles, and what class your Activity extends. – ianhanniballake Aug 17 '14 at 22:38
  • Please post where `@style/AppTheme` is defined – ianhanniballake Aug 17 '14 at 23:24
  • in style.xml file code states – Akshit Rewari Aug 17 '14 at 23:35

2 Answers2

1

Your activity t1 does not have any theme applied to it in the AndroidManifest.xml therefore it uses the default for your application - in your case, @style/AppTheme which includes an Action Bar. Set a theme in your Manifest:

<activity
    android:name=".t1"
    android:screenOrientation="portrait"
    android:theme="@android:style/Theme.Holo.Light.NoActionBar"
>
</activity>

Or make a new theme specifically for the no action bar case:

<style name="AppTheme.NoActionBar" parent="android:Theme.Holo.Light.NoActionBar">
    <!-- Customize your theme here. -->
</style>

And use it:

<activity
    android:name=".t1"
    android:screenOrientation="portrait"
    android:theme="@style/AppTheme.NoActionBar"
>
</activity>
ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • Thanks a lot for your help. I found what I missed out when u asked for styles.xml in comments , I modified my code and it's working now – Akshit Rewari Aug 17 '14 at 23:54
  • However just out of curiosity , if activity t1 does not have any theme applied to it in the AndroidManifest.xml therefore it uses the default @style/AppTheme which includes an Action Bar and I selected no action bar theme in layout_t1.xml design options but did not update manifest, why is there a difference in behaviour of app on nexus 4 and HTC one – Akshit Rewari Aug 17 '14 at 23:58
0

I was able to solve the problem (thanks to help provided by ianhanniballake, Thanks a lot).

Modified styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

<style name="newTheme" parent="android:Theme.Holo.NoActionBar">
    <!-- Customize your theme here. -->
</style>

</resources>

and Modified android manifest

<activity
    android:name=".t1"
    android:screenOrientation="portrait"
    android:theme="newtheme"
>
</activity>
<activity
    android:name=".t2"
    android:screenOrientation="portrait"
            android:theme="newtheme"

>
</activity>
<activity
    android:name=".t3"
    android:screenOrientation="portrait"
    android:theme="newtheme"

    >
</activity>
<activity
    android:name=".t4"
    android:screenOrientation="portrait"
    android:theme="newtheme"

     >
</activity>

Code is working perfectly now on HTC one and NEXUS , Thanks a lot ianhanniballake

Akshit Rewari
  • 941
  • 2
  • 13
  • 26
  • 1
    Make sure you are using `Holo.Light.NoActionBar` instead of `Holo.NoActionBar` otherwise other formatting (such as text coloring, dialogs, etc) will be different from your other activities unless that is specifically what you want – ianhanniballake Aug 17 '14 at 23:59