javaandroideclipseandroid-actionbarandroid-titlebar

Error when the Title Bar is removed


Please have a look at the following code

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

public class Home extends Activity {

    private TextView textView23;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

         //Removing the title bar
         this.requestWindowFeature(Window.FEATURE_NO_TITLE);


         setContentView(R.layout.activity_home);

        //Setting up the Action Bar
         ActionBar actionBar = getActionBar();  //to support lower version too
         actionBar.setDisplayShowCustomEnabled(true);
         View customView=getLayoutInflater().inflate(R.layout.window_title, null);
         actionBar.setCustomView(customView);
         actionBar.setBackgroundDrawable(new ColorDrawable(Color.WHITE));



        textView23 = (TextView)findViewById(R.id.textView23);
        textView23.setSelected(true);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.home, menu);
        return true;
    }

}

I am removing the title bar of the app here. But, as soon as this code get executed, I am getting the following error.

10-30 08:09:58.480: E/AndroidRuntime(2747): FATAL EXCEPTION: main
10-30 08:09:58.480: E/AndroidRuntime(2747): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xx.xx/com.xx.xx.Home}: java.lang.NullPointerException
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.os.Looper.loop(Looper.java:137)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.app.ActivityThread.main(ActivityThread.java:5041)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at java.lang.reflect.Method.invokeNative(Native Method)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at java.lang.reflect.Method.invoke(Method.java:511)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at dalvik.system.NativeStart.main(Native Method)
10-30 08:09:58.480: E/AndroidRuntime(2747): Caused by: java.lang.NullPointerException
10-30 08:09:58.480: E/AndroidRuntime(2747):     at com.xx.xx.Home.onCreate(Home.java:29)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.app.Activity.performCreate(Activity.java:5104)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
10-30 08:09:58.480: E/AndroidRuntime(2747):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
10-30 08:09:58.480: E/AndroidRuntime(2747):     ... 11 more

I am trying to do this because of the following output

enter image description here

As you can see, the app title and my Action bar, both are getting displayed. I do not want the app title, or the title image.


Solution

  • Without Title you can not use ActionBar. So remove the line

    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    

    Your main purpose is to remove title and icon from Title bar then use below code

    ActionBar actionBar = getActionBar();  //to support lower version too
    actionBar.setDisplayShowHomeEnabled(false); // Remove icon
    actionBar.setDisplayShowTitleEnabled(false); // Remove title text
    actionBar.setDisplayShowCustomEnabled(true);
    View customView=getLayoutInflater().inflate(R.layout.window_title, null);
    actionBar.setCustomView(customView);
    actionBar.setBackgroundDrawable(new ColorDrawable(Color.WHITE));