简体   繁体   中英

How to hide action bar on HTC one using Holo.NoActionBar theme

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

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>

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

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