1

I've recently upgraded my application to a material theme. However, I'm encountering crashes on a 4.2.2 Samsung tablet. The stacktrace (posted below) tells me that I am not using a descendant of Theme.AppCompat, even though I believe I am. The app seems to run fine on 4.4 and 5.0.

Here is the stacktrace from the Samsung tablet:

02-23 19:21:29.490: E/AndroidRuntime(20724): FATAL EXCEPTION: main
02-23 19:21:29.490: E/AndroidRuntime(20724): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.name/com.app.name.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
02-23 19:21:29.490: E/AndroidRuntime(20724):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2245)
02-23 19:21:29.490: E/AndroidRuntime(20724):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2295)
02-23 19:21:29.490: E/AndroidRuntime(20724):    at android.app.ActivityThread.access$700(ActivityThread.java:150)
02-23 19:21:29.490: E/AndroidRuntime(20724):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
02-23 19:21:29.490: E/AndroidRuntime(20724):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-23 19:21:29.490: E/AndroidRuntime(20724):    at android.os.Looper.loop(Looper.java:175)
02-23 19:21:29.490: E/AndroidRuntime(20724):    at android.app.ActivityThread.main(ActivityThread.java:5279)
02-23 19:21:29.490: E/AndroidRuntime(20724):    at java.lang.reflect.Method.invokeNative(Native Method)
02-23 19:21:29.490: E/AndroidRuntime(20724):    at java.lang.reflect.Method.invoke(Method.java:511)
02-23 19:21:29.490: E/AndroidRuntime(20724):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
02-23 19:21:29.490: E/AndroidRuntime(20724):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
02-23 19:21:29.490: E/AndroidRuntime(20724):    at dalvik.system.NativeStart.main(Native Method)
02-23 19:21:29.490: E/AndroidRuntime(20724): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
02-23 19:21:29.490: E/AndroidRuntime(20724):    at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
02-23 19:21:29.490: E/AndroidRuntime(20724):    at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
02-23 19:21:29.490: E/AndroidRuntime(20724):    at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
02-23 19:21:29.490: E/AndroidRuntime(20724):    at com.appconstructor.core2.MainActivity.onCreate(MainActivity.java:56)
02-23 19:21:29.490: E/AndroidRuntime(20724):    at com.app.name.MainActivity.onCreate(MainActivity.java:12)
02-23 19:21:29.490: E/AndroidRuntime(20724):    at android.app.Activity.performCreate(Activity.java:5283)
02-23 19:21:29.490: E/AndroidRuntime(20724):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
02-23 19:21:29.490: E/AndroidRuntime(20724):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
02-23 19:21:29.490: E/AndroidRuntime(20724):    ... 11 more

My app uses a library for the activities. The Manifest looks like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.app.name"
    android:versionCode="501"
    android:versionName="5.0.1" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="21" />

        [... Permissions]

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        [... Other activities]  
        [... etc]
    </application>

</manifest>

/res/values/styles.xml is contained in the library and looks like this:

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
  <!-- enable window content transitions -->
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>

As you can see, AppTheme does extend Theme.AppCompat!

There's some other values folders, but none of them seem applicable. I have values-nl for the Dutch translation, values-v19 and values-v21 and then there's values-w820dp which doesn't contain a styles.xml. There's no styles in the main app.

So, I'm obviously missing something. What could it be?\


Update 1: since the tablet is actually really slow, I can see the activity actually loading up as holo. So the problem is that the wrong theme is loaded.

Vic V
  • 1,080
  • 2
  • 12
  • 31
  • your activity does not have a theme, set your activity to your theme – tyczj Feb 23 '15 at 19:00
  • @tyczj explicitly setting a theme on the activity doesn't make a difference. Still loads as holo and therefore crashes :( – Vic V Feb 23 '15 at 19:04
  • @tyczj If the activity doesn't have an explicit theme, it uses the application theme – jmart Sep 03 '15 at 09:53

2 Answers2

2

From my experience, this could be 1 of 2 things (based on the samples you've provided):

  1. When I switched to appcompat, I had this issue a lot, until I found this line on the Android Developers Blog regarding the release of the appcompat support lib You can see the blog post Here:

Migration from previous setup For most apps, you now only need one theme declaration, in values/:

values/themes.xml:

<style name="Theme.MyTheme" parent="Theme.AppCompat.Light">
    <!-- Set AppCompat’s actionBarStyle -->
    <item name="actionBarStyle">@style/MyActionBarStyle</item>

    <!-- Set AppCompat’s color theming attrs -->
    <item name=”colorPrimary”>@color/my_awesome_red</item>
    <item name=”colorPrimaryDark”>@color/my_awesome_darker_red</item>

    <!-- The rest of your attributes -->
</style>

You can now remove all of your values-v14+ Action Bar styles.

It obviously doesn't say you HAVE to remove those other 'values' folders, but doing so helped me.

  1. If you're using appcompat, make sure your activity classes extend ActionBarActivity rather than Activity as mentioned in the same blog post:

New integration If you are not currently using AppCompat, or you are starting from scratch, here's how to set it up:

  • All of your Activities must extend from ActionBarActivity, which extends from FragmentActivity from the v4 support library, so you can continue to use fragments.

  • All of your themes (that want an Action Bar/Toolbar) must inherit from Theme.AppCompat. There are variants available, including Light
    and NoActionBar.

  • When inflating anything to be displayed on the action bar (such as a SpinnerAdapter for list navigation in the toolbar), make sure you use the action bar’s themed context, retrieved via
    getSupportActionBar().getThemedContext().

  • You must use the static methods in MenuItemCompat for any action-related calls on a MenuItem.

Hopefully one of these helps :)

EDIT:

After looking at your project, I see multiple version folders (mdpi-v11, transitions-v21, things like that). Appcompat doesn't work with these for a reason I have yet to discover. Get rid of your version folders, keep all the base ones, and you're good to go.

Psest328
  • 6,575
  • 11
  • 55
  • 90
  • Thanks for the quick reply! I already used #2 correctly. As for #1, I still have some v21-specific items, such as shared transitions. I tried to delete v19 and v21, but even after that, the tablet still crashes.. :( – Vic V Feb 23 '15 at 18:58
  • can you please post your mainActivity and it's layout? – Psest328 Feb 23 '15 at 19:55
  • and do me a favor. Delete all other values folders. you said you have v21 specific stuff. if it's colors, make a new file in the values folder called colors21 put it in there. Only have the 1 values folder. For your dutch translations, use a strings-nl file instead of a whole new values folder – Psest328 Feb 23 '15 at 20:02
  • MainActivity is a small class extending MainActivity in the library: here's mainactivity in the library (minus a few things deemed irrelevant): http://pastebin.com/GYUWUeED. – Vic V Feb 23 '15 at 20:02
  • and can you please paste up R.layout.activity_main? – Psest328 Feb 23 '15 at 20:06
  • I bet dollars to cents the issue is either that you have multiple Values folders or that there's an error in one of your XML folders (Strings, Styles, Themes, etc). You will come to learn the annoyance soon enough that if there is an issue within your XML, appcompat goes batsh*t crazy. Let me know what happens when you've cleared those values – Psest328 Feb 23 '15 at 20:15
  • This is my first time using appcompat, so it probably is something like that :P I just moved all all the values folders outside of my project, so I just have `values` left. Now, my CM12 device crashes as well, with the same stacktrace. The culprit must be somewhere in here: http://pastebin.com/ps8pjdcr (thanks for guiding me through this!) – Vic V Feb 23 '15 at 20:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/71525/discussion-between-wizardknight-and-vic-v). – Psest328 Feb 23 '15 at 20:24
2

I see the problem now, this Theme.AppCompat.Light.DarkActionBar is not a valid theme for toolbar use, that uses the old actionbar. you need to use Theme.AppCompat.Light then when you declare the toolbar you need to change the theme of it

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_my_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

if you want to have the dark actionbar look then see here

also look here for reference

https://chris.banes.me/2014/10/17/appcompat-v21/

Community
  • 1
  • 1
tyczj
  • 71,600
  • 54
  • 194
  • 296
  • Hmm, even though this sounds super logical, it doesn't seem to be it. Still the same error. Also, wouldn't that make it weird that it seems to work on API 19+? – Vic V Feb 23 '15 at 19:22
  • @VicV are you saying changing to this works on 19+ but not below? – tyczj Feb 23 '15 at 19:32
  • I meant that `Theme.AppCompat.Light.DarkActionBar` seems to work on 19+. Here's a screenshot from my Google Play listing: https://lh5.ggpht.com/YtoTv1IsUM7O6nO941qqWB7jKoXs_Cj4mIMISSjoanRdrrBjV7p8FdNVma5sC8oU6g=h900-rw – Vic V Feb 23 '15 at 19:34