Android Studio:无法启动活动ComponentInfo

时间:2017-07-13 16:02:36

标签: java android xml manifest logcat

如果我尝试启动我的应用程序,则会出现以下错误:

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) 

问题应该在这段代码中:

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

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

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

为什么我会收到此错误,如何解决?

我已经尝试更改此行:

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

到这一行:

但这不起作用

这是我的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();


        }

我还尝试将android:theme="@android:style/Theme.Translucent"更改为

android:theme="@style/AppTheme"

然后我收到以下错误:

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.

这是GitHub上的项目: https://github.com/Penta-Games/AngelaMerkel

2 个答案:

答案 0 :(得分:0)

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

在您声明活动的Manifest.xml中,替换主题配置:android:theme="@android:style/Theme.Translucent" with:android:theme="@style/AppTheme"(或从Theme.AppCompat降序的任何其他主题,正如错误消息所示。)

答案 1 :(得分:0)

主要问题在于您的styles.xml文件。

在您的应用中,您只在styles.xml文件夹中添加了/res/values-v21,它仅适用于API21 (LOLLIPOP)higher版本。

由于style.xml文件夹中没有常见的values,并且您尚未为styles版本声明任何特定的lower API,因为它显示{{1}对于IllegalStateException这样的较低版本API 18

<强> SOLUTION:

Android 4.3 (JELLY_BEAN_MR2)文件夹中添加常用styles.xml

/res/values

仅供参考<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> 没有问题。一切似乎都很好。您应该AndroidManifest.xml使用Theme.Translucent

希望这会起作用〜