Amrita Deb
Amrita Deb

Reputation: 335

Kotlin Android app crashes when moving to new activity

This should be simple but I am not sure what I am doing wrong. I want my code to go from one activity to another when a button is clicked.

The current activity is called MainActivity and the activity it redirects to is called 'KickTrend'

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val trend_btn : Button = findViewById(R.id.viewTrend)
        trend_btn.setOnClickListener {
            val intent = Intent(this,KickTrend::class.java)
            Toast.makeText(this, "moving to new Activity", Toast.LENGTH_SHORT).show()
            startActivity(intent)
        }
    }
}

The toast is showing up but the then the app crashes at startActivity(intent) line The Kick Trend activity kotlin file looks like this:

class KickTrend : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_kick_trend)
    }
}

Can anyone tell me what i am doing wrong. I am coming back to Android development after almost 6-7 years and alot has changed so maybe I am missing some syntax or library..I dunno.

The Manifest looks like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.TrackMyBaby"
        tools:targetApi="31">
        <activity android:name=".MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.TrackMyBaby">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

The log cat is as follows:

FATAL EXCEPTION: main Process: com.example.trackmybaby, PID: 19231 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.trackmybaby/com.example.trackmybaby.KickTrend}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4111) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4277) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:226) at android.os.Looper.loop(Looper.java:313) at android.app.ActivityThread.main(ActivityThread.java:8751) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135) Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:926) at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:889) at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:772) at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:197) at com.example.trackmybaby.KickTrend.onCreate(KickTrend.kt:9) at android.app.Activity.performCreate(Activity.java:8290) at android.app.Activity.performCreate(Activity.java:8270) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4085) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4277)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loopOnce(Looper.java:226)  at android.os.Looper.loop(Looper.java:313)  at android.app.ActivityThread.main(ActivityThread.java:8751)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135)

Upvotes: 0

Views: 632

Answers (1)

Mikhail Guliaev
Mikhail Guliaev

Reputation: 1648

The error message you're encountering indicates that the theme you've set for your KickTrend activity is not compatible with the AppCompat theme requirements. The AppCompatActivity class (which KickTrend extends) requires that the theme used in the activity is a descendant of Theme.AppCompat. I suppose you can do following things to fix the issue:

  1. Extend your MainActivity from AppCompatActivity, not the ComponentActivity

  2. Make sure that your Theme.TrackMyBaby is extended from AppCompat theme, not the regular one. You cannot use non-appcompat themes for AppCompatActivity. Like this

     <resources>
     <style name="Theme.TrackMyBaby" parent="Theme.AppCompat.Light">
         <!-- ... theme attributes ... -->
     </style>
    
  3. Just in case you can define a theme in AndroidManifest.xml for your KickTrend activity.

     <!-- AndroidManifest.xml -->
     <activity
         android:name=".KickTrend"
         android:theme="@style/Theme.AppCompat.Light">
     </activity>
    

Upvotes: 1

Related Questions