Search code examples
javaandroidxmlmanifestlogcat

Android Studio: Unable to start activity ComponentInfo


If I try to start my Application following error appears:

07-13 17:51:04.663 23276-23276/com.aaron.waller.angelasoundboard E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aaron.waller.angelasoundboard/com.aaron.waller.angelasoundboard.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
    at android.app.ActivityThread.access$700(ActivityThread.java:159)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5419)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
    at android.support.v7.app.AppCompatDelegateImplV9.createSubDecor(AppCompatDelegateImplV9.java:355)
    at android.support.v7.app.AppCompatDelegateImplV9.ensureSubDecor(AppCompatDelegateImplV9.java:324)
    at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:285)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
    at com.aaron.waller.angelasoundboard.MainActivity.onCreate(MainActivity.java:40)
    at android.app.Activity.performCreate(Activity.java:5372)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349) 
    at android.app.ActivityThread.access$700(ActivityThread.java:159) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:5419) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:525) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) 
    at dalvik.system.NativeStart.main(Native Method)

 

The issue should be in this code:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
}

my activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">





    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorAccent"
        android:id="@+id/toolbar"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:title="Angela Merkel Soundboard" />

    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/drawerLayout">




        <FrameLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/containerView">



            <com.google.android.gms.ads.AdView
                android:id="@+id/adView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_alignParentBottom="true"
                ads:adSize="SMART_BANNER"
                android:layout_gravity="bottom|end"
                ads:adUnitId="@string/banner_ad_unit_id">
            </com.google.android.gms.ads.AdView>

        </FrameLayout>




        <android.support.design.widget.NavigationView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:id="@+id/shitstuff"
            app:itemTextColor="@color/black"
            app:menu="@menu/drawermenu"
            />



    </android.support.v4.widget.DrawerLayout>





</LinearLayout>

My Manifest.xml:

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



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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name="com.aaron.waller.angelasoundboard.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>
        <activity android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" />
    </application>

</manifest>

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

Why do I get this error and how can I solve it?

I already tried to change this line:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

to this line:

but this is not working

This is whole my MainActivity.java:

public class MainActivity extends AppCompatActivity {
    public MediaPlayer mp;
    DrawerLayout mDrawerLayout;
    NavigationView mNavigationView;
    FragmentManager mFragmentManager;
    FragmentTransaction mFragmentTransaction;
    public InterstitialAd mInterstitialAd;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

        mInterstitialAd = new InterstitialAd(MainActivity.this);
        mInterstitialAd.setAdUnitId("ca-app-pub-2859881364147521/9737493693");
        mInterstitialAd.loadAd(adRequest);
        mInterstitialAd.setAdListener(new AdListener() {
            // Listen for when user closes ad
            public void onAdClosed() {
                // When user closes ad end this activity (go back to first activity)
                super.onAdClosed();

            }
        });

        final File FILES_PATH = new File(Environment.getExternalStorageDirectory(), "Android/data/com.aaron.waller.angelasoundboard/files");


        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {

            if (!FILES_PATH.mkdirs()) {

                Log.w("error", "Could not create " + FILES_PATH);

            }

        } else {

            Toast.makeText(MainActivity.this, "error", Toast.LENGTH_LONG).show();

            finish();

        }


        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE},
                1);



            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
            mNavigationView = (NavigationView) findViewById(R.id.shitstuff);


            mFragmentManager = getSupportFragmentManager();
            mFragmentTransaction = mFragmentManager.beginTransaction();
            mFragmentTransaction.replace(R.id.containerView, new TabFragment()).commit();

            mNavigationView.setItemIconTintList(null);


            mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(MenuItem menuItem) {
                    mDrawerLayout.closeDrawers();


                    if (menuItem.getItemId() == R.id.nav_item_inbox) {
                        FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                        fragmentTransaction.replace(R.id.containerView, new SentFragment()).commit();

                    }
                    if (menuItem.getItemId() == R.id.teilen) {
                        Intent shareIntent = new Intent(Intent.ACTION_SEND);
                        shareIntent.setType("text/plain");
                        shareIntent.putExtra(Intent.EXTRA_SUBJECT, "✶Angela Merkel Soundboard✶");
                        shareIntent.putExtra(Intent.EXTRA_TEXT, "✶Hol dir das Angela Merkel Soundboard mit Merkels besten Sprüchen✶\n\nhttps://play.google.com/store/apps/details?id=com.aaron.waller.angelasoundboard&hl=de");
                        startActivity(Intent.createChooser(shareIntent, "Teilen über..."));
                    }


                    if (menuItem.getItemId() == R.id.nav_item_inbox) {
                        FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                        xfragmentTransaction.replace(R.id.containerView, new TabFragment()).commit();
                    }

                    if (menuItem.getItemId() == R.id.instagram) {
                        String url = "https://instagram.com/_u/coding.empire/?r=sun1";

                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_VIEW);
                        intent.setData(Uri.parse(url));
                        onPause();
                        startActivity(intent);
                    }

                    if (menuItem.getItemId() == R.id.freshtorge) {
                        String url = "https://play.google.com/store/apps/details?id=com.penta.games.freshtorgesoundboard&hl=de";

                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_VIEW);
                        intent.setData(Uri.parse(url));
                        onPause();
                        startActivity(intent);
                    }
                    if (menuItem.getItemId() == R.id.tanzverbot) {
                        String url = "https://play.google.com/store/apps/details?id=com.aaron.waller.tanzverbotsoundboard&hl=de";

                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_VIEW);
                        intent.setData(Uri.parse(url));
                        onPause();
                        startActivity(intent);
                    }
                    if (menuItem.getItemId() == R.id.elotrix) {
                        String url = "https://play.google.com/store/apps/details?id=com.penta.games.elotrixsoundboard&hl=de";

                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_VIEW);
                        intent.setData(Uri.parse(url));
                        onPause();
                        startActivity(intent);
                    }
                    if (menuItem.getItemId() == R.id.gronkh) {
                        String url = "https://play.google.com/store/apps/details?id=com.aaron.waller.gronkhsoundboard&hl=de";

                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_VIEW);
                        intent.setData(Uri.parse(url));
                        onPause();
                        startActivity(intent);
                    }
                    if (menuItem.getItemId() == R.id.dummefage) {
                        String url = "https://play.google.com/store/apps/details?id=com.penta.games.dummefragenet&hl=de";

                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_VIEW);
                        intent.setData(Uri.parse(url));
                        onPause();
                        startActivity(intent);
                    }
                    if (menuItem.getItemId() == R.id.email) {
                        Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                                "mailto", "pentagames.business@gmail.com", null));
                        ;
                        startActivity(Intent.createChooser(emailIntent, "E-Mail senden..."));
                    }


                    return false;
                }

            });


            android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
            ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, toolbar, R.string.app_name,
                    R.string.app_name);

            mDrawerLayout.setDrawerListener(mDrawerToggle);

            mDrawerToggle.syncState();


        }

I also tried to change android:theme="@android:style/Theme.Translucent" to

android:theme="@style/AppTheme"

but then I get following error:

Error:Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed : Attribute activity#com.google.android.gms.ads.AdActivity@theme value=(@style/AppTheme) from AndroidManifest.xml:28:13-44
    is also present at [com.google.android.gms:play-services-ads-lite:10.2.0] AndroidManifest.xml:25:182-230 value=(@android:style/Theme.Translucent).
    Suggestion: add 'tools:replace="android:theme"' to <activity> element at AndroidManifest.xml:26:9-28:47 to override.

This is the project on GitHub: https://github.com/Penta-Games/AngelaMerkel


Solution

  • The main problem is with your styles.xml file.

    In your application, you have added only styles.xml in your /res/values-v21 folder and it will work only for API level 21 (LOLLIPOP) and higher versions.

    As there is no common style.xml in values folder and you have not declared any specific styles for lower API version that's why its showing IllegalStateException for lower versions like API 18 which is Android 4.3 (JELLY_BEAN_MR2).

    SOLUTION:

    Add an common styles.xml in your /res/values folder.

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

    FYI, There is no issues in your AndroidManifest.xml. Everything seems fine. You should use Theme.Translucent for "com.google.android.gms.ads.AdActivity".

    Hope this will work~