Search code examples
javaandroidzxing

Error: You need to use a Theme.AppCompat theme (or descendant) with this activity


I am getting this error while executing (on the android monitor console):

06-16 11:25:38.227 1555-1555/com.example.mysampleapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mysampleapp/com.example.mysampleapp.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

I am trying to integrate zxing in my app using this project, with instructions here. My Manifest is this:

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

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:name="android.support.multidex.MultiDexApplication"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

            <meta-data
                android:name="com.google.android.geo.API_KEY"
                android:value="randomkeywhichyoudontneedtoknow" />
        </activity>

        <activity
            android:name=".SplashActivity"
            android:label="mysampleapp"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize"></activity>

    </application>

</manifest>

Styles.xml:

<resources>

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

</resources>

(needed to add some text to the question, so stackoverflow is satisfied and here it is!)


Solution

  • Try removing this line:

    android:theme="@style/AppTheme.NoActionBar"

    from your Activity.

    As your style file contains, your AppTheme is already using Theme.AppCompat.Light.NoActionBar, and your manifest already applies it application-wide.