Search code examples
javaandroidkotlintoolbar

Getting java.lang.IllegalStateException during onCreate of MainActivity.kt


I am unable to reproduce the error below myself, but in Google Play there are over 400 occurrences for this issue (I've tried on various Samsung and Pixel devices and still can't reproduce with SDK 30, 31, & 32):

java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:4035)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:4201)
  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:2438)
  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:8663)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:567)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1135)
Caused by: java.lang.IllegalStateException: 
  at androidx.appcompat.app.AppCompatDelegateImpl.setSupportActionBar (AppCompatDelegateImpl.java:581)
  at androidx.appcompat.app.AppCompatActivity.setSupportActionBar (AppCompatActivity.java:183)
  at org.fwac.fireweatheravalanche.MainActivity.onCreate (MainActivity.kt:319)
  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:4009)

Here is the Kotlin code for MainActivity.kt (line 319 is the setSupportActionBar() function):

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val toolbar: Toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)

This is the layout file for MainActivity.kt

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/toolbarContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Theme.FireWeatherAvalancheCenter.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/Theme.FireWeatherAvalancheCenter.PopupOverlay" />

    </com.google.android.material.appbar.AppBarLayout>

    <include layout="@layout/content_main" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

I've been searching all over the internet for a solution, but can't find one. Like I said, can't reproduce this issue so I'm left unable to make any changes. Would really appreciate any help!!!


Solution

  • I read your code and source code, the reason why it reports IllegalStateException is that it already has a actionBar.

    final ActionBar ab = getSupportActionBar();
     if (ab instanceof WindowDecorActionBar) {
           throw new IllegalStateException("This Activity already has an action bar supplied " +
           "by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set " +
        "windowActionBar to false in your theme to use a Toolbar instead.");
      }
    
    

    As for some devices reporting the error, it may the default theme is not same. you can try to set the windowActionBar false in your theme

     <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
            <!-- Customize your theme here. -->
            <item name="android:windowActionBar">false</item>
      </style>