Android getActionBar 与 getSupportActionBar?
Android getActionBar vs getSupportActionBar?
这是我的代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
//android.support.v7.app.ActionBar actionBar = getSupportActionBar();
//actionBar.setTitle("Android");
ActionBar actionBar = getActionBar();
actionBar.setTitle("Droid");
}
在使用 getSupportActionBar() 时,我的应用程序在 kitkat 和其他新版本上运行得很好,但使用 getActionBar 会导致错误。
这是错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.hide()' on a null object reference
at com.github.domain.geoquiz.QuizActivity.onCreate(QuizActivity.java:57)
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.access0(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
为什么?来自 android 文档:
Caution: Be certain you import the ActionBar class (and related APIs)
from the appropriate package:
If supporting API levels lower than 11:
import android.support.v7.app.ActionBar
If supporting only API level
11 and higher: import android.app.ActionBar
为什么这个应用会崩溃?
如果您正在使用 AppCompat,无论您的应用程序是哪个 Android 版本,您总是必须调用 getSupportActionBar()
运行。
您的目标 Android 版本是什么?
我建议您使用新的 Toolbar 而不是 ActionBar,因为它使用起来更灵活。
如果您要扩展 AppCompatActivity
,那么您就是在为旧的 Android 版本提供向后支持,为此您必须使用 getSupportActionBar()
.
如果您要导入 android.app.ActionBar
那么您必须使用 getActionBar()
如果您要导入 android.support.v7.app.ActionBar
那么你必须使用 getSupportActionBar()
。
注意: 通过使用 android.support.v7.app.ActionBar
,您可以为旧的 Android 版本提供向后支持。
这是我的代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
//android.support.v7.app.ActionBar actionBar = getSupportActionBar();
//actionBar.setTitle("Android");
ActionBar actionBar = getActionBar();
actionBar.setTitle("Droid");
}
在使用 getSupportActionBar() 时,我的应用程序在 kitkat 和其他新版本上运行得很好,但使用 getActionBar 会导致错误。
这是错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.hide()' on a null object reference
at com.github.domain.geoquiz.QuizActivity.onCreate(QuizActivity.java:57)
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.access0(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
为什么?来自 android 文档:
Caution: Be certain you import the ActionBar class (and related APIs) from the appropriate package:
If supporting API levels lower than 11: import android.support.v7.app.ActionBar
If supporting only API level 11 and higher: import android.app.ActionBar
为什么这个应用会崩溃?
如果您正在使用 AppCompat,无论您的应用程序是哪个 Android 版本,您总是必须调用 getSupportActionBar()
运行。
您的目标 Android 版本是什么?
我建议您使用新的 Toolbar 而不是 ActionBar,因为它使用起来更灵活。
如果您要扩展 AppCompatActivity
,那么您就是在为旧的 Android 版本提供向后支持,为此您必须使用 getSupportActionBar()
.
如果您要导入 android.app.ActionBar
那么您必须使用 getActionBar()
如果您要导入 android.support.v7.app.ActionBar
那么你必须使用 getSupportActionBar()
。
注意: 通过使用 android.support.v7.app.ActionBar
,您可以为旧的 Android 版本提供向后支持。