简体   繁体   中英

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

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

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");

Or directly you can use ,

getSupportActionBar().setTitle("Your new title here!");
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

and other methods...

使用支持方法:

ActionBar actionBar = getSupportActionBar();

更改活动以扩展ActionBarActivity即可

  1. have your class extend AppCompatActivity
  2. put "requestWindowFeature(Window.FEATURE_ACTION_BAR);" before "super.onCreate(savedInstanceState);" in your onCreate method
  3. use "getSupportActionBar()" instead of "getActionBar()"

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