2015-09-01 75 views
0

我想使用ActionBar,我檢查了一些postes,並且我編寫了下面的代碼,但在運行時我收到下面的logCat輸出。我知道這是關於應用程序的主題,我試圖設置一個新的主題,如下面的代碼所示,但它也不起作用。如何設置一個主題使用ActionBar

請讓我們現在如何解決它。

代碼

private void initActionBar() { 
    // TODO Auto-generated method stub 
    //setTheme(android.R.style.Theme_Holo); 
    getSupportActionBar().setTitle(R.string.action_discover_title); 
} 

style.xml

<resources> 
<!-- 
    Base application theme, dependent on API level. This theme is replaced 
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices. 
--> 
<style name="AppBaseTheme" parent="android:Theme.Light"> 
    <!-- 
     Theme customizations available in newer API levels can go in 
     res/values-vXX/styles.xml, while customizations related to 
     backward-compatibility can go here. 
    --> 
</style> 

<!-- Application theme. --> 
<style name="AppTheme" parent="AppBaseTheme"> 
    <!-- All customizations that are NOT specific to a particular API-level can go here. --> 
</style> 
</resources> 

logcat的

09-01 17:35:46.237: E/AndroidRuntime(19987): FATAL EXCEPTION: main 
09-01 17:35:46.237: E/AndroidRuntime(19987): Process: com.example.ble_00, PID: 19987 
09-01 17:35:46.237: E/AndroidRuntime(19987): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ble_00/com.example.ble_00.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 
09-01 17:35:46.237: E/AndroidRuntime(19987): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2689) 
09-01 17:35:46.237: E/AndroidRuntime(19987): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2754) 
09-01 17:35:46.237: E/AndroidRuntime(19987): at android.app.ActivityThread.access$900(ActivityThread.java:177) 
09-01 17:35:46.237: E/AndroidRuntime(19987): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
09-01 17:35:46.237: E/AndroidRuntime(19987): at android.os.Handler.dispatchMessage(Handler.java:102) 

回答

1

因爲你需要一個ppcompat主題使用ActionBar。將parent="android:Theme.Light"更改爲任何Appcompat主題作爲您的外觀和感覺首選項。 如:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light"> 
    <!-- 
     can override the default look and feel of the parent theme 
    --> 
</style> 

您可以查看更多樣式的ActionBar here

並在您要使用Appcomapt的活動中使用該主題。您可以在AndroidManifest.xml文件

<activity 
     android:name="com.YourActivity" 
     android:label="@string/label" 
     android:theme="@style/AppBaseTheme"/> 
1

指定它這樣的:

<style name="AppBaseTheme" parent="android:Theme.Light"> 
    <!-- 
     Theme customizations available in newer API levels can go in 
     res/values-vXX/styles.xml, while customizations related to 
     backward-compatibility can go here. 
    --> 
</style> 

應該是:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light"> 
    <!-- 
     Theme customizations available in newer API levels can go in 
     res/values-vXX/styles.xml, while customizations related to 
     backward-compatibility can go here. 
    --> 
</style> 
相關問題