java Android Studio:无法启动活动 ComponentInfo
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45085879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Studio: Unable to start activity ComponentInfo
提问by OverflowStacker
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.access0(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.access0(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:
我的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:
我的 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:
还有我的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:
这是我的 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", "[email protected]", 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="@android:style/Theme.Translucent"为
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
这是 GitHub 上的项目:https: //github.com/Penta-Games/AngelaMerkel
回答by Devsil
You need to use a Theme.AppCompat theme (or descendant) with this activity.
In your Manifest.xml, where you declare the Activity, replace the theme config: android:theme="@android:style/Theme.Translucent"with: android:theme="@style/AppTheme"(or any other theme descending from Theme.AppCompat, just as the error message reads).
在您Manifest.xml声明 Activity 的 中,将主题 config: 替换为android:theme="@android:style/Theme.Translucent":(android:theme="@style/AppTheme"或任何其他从 下降的主题Theme.AppCompat,正如错误消息所显示的那样)。
回答by Ferdous Ahamed
The main problem is with your styles.xmlfile.
主要问题出在您的styles.xml文件上。
In your application, you have added only styles.xmlin your /res/values-v21folder and it will work only for APIlevel 21 (LOLLIPOP)and higherversions.
在您的应用程序中,您仅添加styles.xml到您的/res/values-v21文件夹中,它仅适用于API级别21 (LOLLIPOP)和higher版本。
As there is no common style.xmlin valuesfolder and you have not declared any specific stylesfor lower APIversion that's why its showing IllegalStateExceptionfor lower versions like API 18which is Android 4.3 (JELLY_BEAN_MR2).
由于没有共同style.xml的values文件夹,你还没有宣布任何具体styles的lower API版本,这就是为什么它的表现IllegalStateException为较低的版本就像API 18是Android 4.3 (JELLY_BEAN_MR2)。
SOLUTION:
解决方案:
Add an common styles.xmlin your /res/valuesfolder.
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>
FYI, There is no issues in your AndroidManifest.xml. Everything seems fine. You should use Theme.Translucentfor "com.google.android.gms.ads.AdActivity".
仅供参考,您的AndroidManifest.xml. 一切似乎都很好。你应该使用Theme.Translucentfor "com.google.android.gms.ads.AdActivity"。
Hope this will work~
希望这会奏效~

