ActionBar没有显示在android中

时间:2015-11-30 07:21:41

标签: android android-actionbar

我正在努力学习android开发课程。当我在模拟器上运行应用程序时,action bar未显示。

我有另一个由主要活动触发的活动。如果我不在android_manifest文件中添加以下行,那么在主要活动中单击listview时会出现以下错误

  

java.lang.IllegalStateException:您需要在此活动中使用Theme.AppCompat主题(或后代)。

所以我搜索了这个错误,我得到了App-Compat应该被使用。 所以我在android_manifest文件

中添加了以下这一行
<application
....
....
  android:theme="@style/Theme.AppCompat"
>.....</application>

因此,在添加此行后,我会看到没有操作栏的关注屏幕。 ScreenShot

当我删除android:theme="@style/Theme.AppCompat manifest文件中的行时,我会收到操作栏,但点击列表视图后会显示错误

修改

这是我的styles.xml文件

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
</style>

修改

我的清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.suraj.sunshine">

  <uses-permission android:name="android.permission.INTERNET" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>


    <activity
        android:name=".DetailedActivity"
        android:label="@string/title_activity_detailed"
        android:parentActivityName=".MainActivity"
        >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.suraj.sunshine.MainActivity" />
    </activity>

    <activity
        android:name=".SettingsActivity"
        android:label="@string/title_activity_detailed"
        android:parentActivityName=".MainActivity"
        >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.suraj.sunshine.MainActivity" />
    </activity>
 </application>

</manifest>

2 个答案:

答案 0 :(得分:0)

在Manifest中尝试使用此代码。

//使用 android:theme =“@ style / AppTheme”而不是 android:theme =“@ style / Theme.AppCompat”

<application
    ....
    ....
      android:theme="@style/AppTheme"
    >.....</application>

答案 1 :(得分:0)

style.xml

中试试这个
 <style name="AppTheme" parent="AppBaseTheme">

在清单中:

<application android:theme ="@style/AppTheme"> 

</application>

希望它有所帮助。

相关问题