How can I get ActionBar from Main(ActionBarActivity) intent Activity

Ymow Wu picture Ymow Wu · Jan 14, 2015 · Viewed 10.5k times · Source

I use ActionBarActivity intent a new Activity but I cant call ActionBar method in Activity

//        requestWindowFeature(Window.FEATURE_ACTION_BAR);
//        getActionBar().setTitle("123");
        ActionBar actionBar =getActionBar();
//
        actionBar.setDisplayHomeAsUpEnabled(true);

will get null

01-14 16:29:58.712    3405-3405/com.fragment.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.fragment.test, PID: 3405
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.fragment.test/com.fragment.test.Ac1}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
            at com.fragment.test.Ac1.onCreate(Ac1.java:15)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
01-14 16:30:01.233    3405-3405/com.fragment.test I/Process﹕ Sending signal. PID: 3405 SIG: 9
01-14 17:12:11.968    3795-3795/com.fragment.test I/Process﹕ Sending signal. PID: 3795 SIG: 9

Can I get ActionBar without change the Activity to ActionBarActivity thanks

Answer

Guihgo picture Guihgo · Jan 14, 2015

if you use SUPPORT LIBRARY ► android.support.v7.app.ActionBar

you have to get the support ActionBar.

so,

ActionBar actionBar = getSupportActionBar();

so, you can set title, subtitle and others functions (see all just clicking ►HERE◄)

Like:

actionBar.setTitle("My App Title");