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