如何从Main(ActionBarActivity)intent Activity获取ActionBar

时间:2015-01-14 17:49:36

标签: android android-intent android-actionbar android-actionbaractivity

我使用ActionBarActivity意图一个新的Activity 但我不能在Activity

中调用ActionBar方法
//        requestWindowFeature(Window.FEATURE_ACTION_BAR);
//        getActionBar().setTitle("123");
        ActionBar actionBar =getActionBar();
//
        actionBar.setDisplayHomeAsUpEnabled(true);

将获得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

我可以在不将Action更改为ActionBarActivity的情况下获取ActionBar 感谢

5 个答案:

答案 0 :(得分:7)

如果您使用SUPPORTLIBRARY►android.support.v7.app.ActionBar

你必须得到ActionBar的支持。

所以,

ActionBar actionBar = getSupportActionBar();

因此,您可以设置标题,副标题和其他功能(see all just clicking ►HERE◄)

像:

actionBar.setTitle("My App Title");

答案 1 :(得分:1)

可以直接使用

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

和其他方法......

答案 2 :(得分:0)

使用支持方式:

ActionBar actionBar = getSupportActionBar();

答案 3 :(得分:0)

更改活动以扩展ActionBarActivity及其工作

答案 4 :(得分:0)

  1. 让您的课程扩展AppCompatActivity
  2. put" requestWindowFeature(Window.FEATURE_ACTION_BAR);"之前" super.onCreate(savedInstanceState);"在你的onCreate方法
  3. 使用" getSupportActionBar()"而不是" getActionBar()"