"你需要使用一个主题.AppCompat"?无法运行

问题描述 投票:0回答:1

我的应用程序出现了一个问题,它说:"你需要使用一个Theme.AppCompat主题(或后代)与这个活动。"

这是清单。

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

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

这是主活动

class MainActivity : AppCompatActivity() {
//overide
 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

Intent(this, MusicService::class.java).also { intent ->
    startService(intent)
}

    val db = SongDatabase.getDatabase(application)/* var onClick = View.OnClickListener().     {view ->
    var mainActivity : activity_main= activity as
    mainActivity.player_fragment()
}*/
}

这里是xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout     xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/player_fragment"
        class="com.example.ema_music_app.PlayerFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="9"
        app:layout_constraintBottom_toTopOf="@id/menulayout"
        tools:layout="@layout/player_fragment" />

任何帮助都非常感激,我已经读了所有之前的相关问题,感觉就像这里所有关于这个错误的问题,它让我疯狂... ...干杯。

风格

 <!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>
android android-studio kotlin android-xml android-appcompat
1个回答
0
投票

我将稍微扩展一下Francesc的答案。

这里是安装了应用程序的主题,你安装不明白什么。

android:theme="@style/AppCompat"

错误说明你的主题应该继承自Theme.AppCompat,但没有这样调用。

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>

这个样式是继承自主题的,所以你可以把它作为应用程序的主题.这是由一行代码表示的。<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

因此,清单会是这样的。

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

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

2
投票

变化

android:theme="@style/AppCompat"

android:theme="@style/AppTheme"
© www.soinside.com 2019 - 2024. All rights reserved.