隐藏标题栏/操作栏的最佳方法是什么?

时间:2015-03-25 07:08:08

标签: android android-actionbar

我想在横向模式下隐藏标题栏(操作栏),所以我使用此代码:

if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
           requestWindowFeature(Window.FEATURE_NO_TITLE);
        }
        setContentView(R.layout.activity_view_entries);

这适用于大多数设备,但它会在HTC Sensation XL(http://www.gsmarena.com/htc_sensation_xl-4161.php)上崩溃应用程序。

这是style.xml(我在清单中使用CustomActionBarTheme):

<style name="CustomActionBarTheme"
           parent="@android:style/Theme.Holo.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="android:dropDownSpinnerStyle">@style/SpinnerAppTheme</item>
        <item name="android:editTextBackground">@drawable/apptheme_edit_text_holo_light</item>
    </style>

    <style name="MyActionBar"
           parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@color/orange</item>
        <item name="android:icon">@drawable/ic_action_logo</item>
    </style>

这是崩溃日志:

03-24 14:51:39.129 E/AndroidRuntime(13890): FATAL EXCEPTION: main
03-24 14:51:39.129 E/AndroidRuntime(13890): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myprojects.tib.abstimetracker/com.myprojects.tib.abstimetracker.activities.ViewEntries}: java.lang.NullPointerException
03-24 14:51:39.129 E/AndroidRuntime(13890):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
03-24 14:51:39.129 E/AndroidRuntime(13890):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
03-24 14:51:39.129 E/AndroidRuntime(13890):     at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3791)
03-24 14:51:39.129 E/AndroidRuntime(13890):     at android.app.ActivityThread.access$700(ActivityThread.java:139)
03-24 14:51:39.129 E/AndroidRuntime(13890):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
03-24 14:51:39.129 E/AndroidRuntime(13890):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-24 14:51:39.129 E/AndroidRuntime(13890):     at android.os.Looper.loop(Looper.java:154)
03-24 14:51:39.129 E/AndroidRuntime(13890):     at android.app.ActivityThread.main(ActivityThread.java:4945)
03-24 14:51:39.129 E/AndroidRuntime(13890):     at java.lang.reflect.Method.invokeNative(Native Method)
03-24 14:51:39.129 E/AndroidRuntime(13890):     at java.lang.reflect.Method.invoke(Method.java:511)
03-24 14:51:39.129 E/AndroidRuntime(13890):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-24 14:51:39.129 E/AndroidRuntime(13890):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-24 14:51:39.129 E/AndroidRuntime(13890):     at dalvik.system.NativeStart.main(Native Method)
03-24 14:51:39.129 E/AndroidRuntime(13890): Caused by: **java.lang.NullPointerException**
03-24 14:51:39.129 E/AndroidRuntime(13890):     at com.android.internal.policy.impl.PhoneWindow$PanelFeatureState.onRestoreInstanceState(PhoneWindow.java:3493)
03-24 14:51:39.129 E/AndroidRuntime(13890):     at com.android.internal.policy.impl.PhoneWindow.restorePanelState(PhoneWindow.java:1733)
03-24 14:51:39.129 E/AndroidRuntime(13890):     at com.android.internal.policy.impl.PhoneWindow.restoreHierarchyState(PhoneWindow.java:1689)
03-24 14:51:39.129 E/AndroidRuntime(13890):     at android.app.Activity.onRestoreInstanceState(Activity.java:939)
03-24 14:51:39.129 E/AndroidRuntime(13890):     at android.app.Activity.performRestoreInstanceState(Activity.java:911)
03-24 14:51:39.129 E/AndroidRuntime(13890):     at android.app.Instrumentation.callActivityOnRestoreInstanceState(Instrumentation.java:1132)
03-24 14:51:39.129 E/AndroidRuntime(13890):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2172)
03-24 14:51:39.129 E/AndroidRuntime(13890):     ... 12 more

我的问题是:任何人都可以告诉我应该检查什么是null以及标题栏和操作栏之间有什么区别?

1 个答案:

答案 0 :(得分:1)

标题栏是UI的一小部分,您可以提供一些文本和颜色。你在许多Android 2.0应用程序上看到它。见https://androidproblem.wordpress.com/category/title-bar/

操作栏是带有按钮的栏,后面有导航等。如果可以选择,则使用它而不是标题栏。见https://developer.android.com/guide/topics/ui/actionbar.html

检查空值,你可以找到哪一行给你一个空指针异常,然后你可以添加一个if检查,如下所示:

if ( x != null){

//perform actual action

}

else {

// Display a toast or some thing and perform a graceful exit from the application or stay in the application.

}

希望这会有所帮助。差异基于之前的SO答案。