Paddy
Paddy

Reputation: 57

"You need to use a theme.AppCompat"?? Wont run

got an issue with my app crashing, it's saying "You need to use a Theme.AppCompat theme (or descendant) with this activity."

Heres the manifest:

    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>

Heres the main activity:

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()
}*/
}

Here is the 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" />

any helps greatly appreciated, I've read what feels like all the previously related questions here regarding this error and its driving me mad.. cheers

Styles

 <!-- 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>

Upvotes: 0

Views: 425

Answers (2)

Razrab.ytka
Razrab.ytka

Reputation: 101

I will slightly expand the answer Francesc.

Here the theme of the application is installed, you install do not understand what.

android:theme="@style/AppCompat"

The error indicates that your topic should be inherited from Theme.AppCompat but not called so.

<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>

This style is inherited from the theme, so you can put it as an application theme. This is indicated by a line of code<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

As a result, the manifest will look like this:

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>

Upvotes: 0

Francesc
Francesc

Reputation: 29290

Change

android:theme="@style/AppCompat"

to

android:theme="@style/AppTheme"

Upvotes: 2

Related Questions