我的活动扩展了AppCompatActivity并在启动

时间:2015-10-16 10:17:12

标签: android android-theme appcompatactivity

我正在创建一个应用程序,我收到错误,

启动它崩溃并说

  

引起:java.lang.IllegalStateException:你需要使用一个   具有此活动的Theme.AppCompat主题(或后代)。

这是我的活动:

公共类SplashScreen扩展了AppCompatActivity {

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

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(SplashScreen.this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(intent);
            finish();
        }
    }, 3000);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_splash_screen, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

}

 <!-- Base application theme. -->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowAnimationStyle">@style/MyAnimation.Window</item>
    <item name="android:windowBackground">@color/background</item>

</style>

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

这是我的清单:

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

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

这是完整的错误日志:

  

致命例外:主要10-16 15:56:26.135   25342-25342 / com.social_infotech.renthouse E / AndroidRuntime:   java.lang.RuntimeException:无法启动活动   ComponentInfo {com.social_infotech.renthouse / com.social_infotech.renthouse.activity.SplashScreen}:   java.lang.IllegalStateException:您需要使用Theme.AppCompat   主题(或后代)与此活动。 10-16 15:56:26.135   25342-25342 / com.social_infotech.renthouse E / AndroidRuntime:at   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)   10-16 15:56:26.135 25342-25342 / com.social_infotech.renthouse   E / AndroidRuntime:at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)   10-16 15:56:26.135 25342-25342 / com.social_infotech.renthouse   E / AndroidRuntime:at   android.app.ActivityThread.access $ 600(ActivityThread.java:141)10-16   15:56:26.135 25342-25342 / com.social_infotech.renthouse   E / AndroidRuntime:at   android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1256)   10-16 15:56:26.135 25342-25342 / com.social_infotech.renthouse   E / AndroidRuntime:at   android.os.Handler.dispatchMessage(Handler.java:99)10-16 15:56:26.135   25342-25342 / com.social_infotech.renthouse E / AndroidRuntime:at   android.os.Looper.loop(Looper.java:137)10-16 15:56:26.135   25342-25342 / com.social_infotech.renthouse E / AndroidRuntime:at   android.app.ActivityThread.main(ActivityThread.java:5103)10-16   15:56:26.135 25342-25342 / com.social_infotech.renthouse   E / AndroidRuntime:at java.lang.reflect.Method.invokeNative(Native   方法)10-16 15:56:26.135 25342-25342 / com.social_infotech.renthouse   E / AndroidRuntime:at   java.lang.reflect.Method.invoke(Method.java:525)10-16 15:56:26.135   25342-25342 / com.social_infotech.renthouse E / AndroidRuntime:at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:737)   10-16 15:56:26.135 25342-25342 / com.social_infotech.renthouse   E / AndroidRuntime:at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)10-16   15:56:26.135 25342-25342 / com.social_infotech.renthouse   E / AndroidRuntime:at dalvik.system.NativeStart.main(Native Method)   10-16 15:56:26.135 25342-25342 / com.social_infotech.renthouse   E / AndroidRuntime:引起:java.lang.IllegalStateException:你   需要使用Theme.AppCompat主题(或后代)   活动。 10-16 15:56:26.135 25342-25342 / com.social_infotech.renthouse   E / AndroidRuntime:at   android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:112)   10-16 15:56:26.135 25342-25342 / com.social_infotech.renthouse   E / AndroidRuntime:at   android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:148)   10-16 15:56:26.135 25342-25342 / com.social_infotech.renthouse   E / AndroidRuntime:at   android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:60)   10-16 15:56:26.135 25342-25342 / com.social_infotech.renthouse   E / AndroidRuntime:at   com.social_infotech.renthouse.activity.SplashScreen.onCreate(SplashScreen.java:16)

5 个答案:

答案 0 :(得分:0)

使用FragmentActivity或Activity扩展您的活动 因为您使用NoAcionBar作为主题。

答案 1 :(得分:0)

表示noActionBar活动:

<强> style.xml

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

</style>

<强>的AndroidManifest.xml

<activity
   android:name=".SplashScreenActivity"
   android:theme="@style/AppThemeNoActionBar" >
</activity>

<强> SplashScreen.java

import android.support.v7.app.AppCompatActivity;
public class SplashscreenActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splashscreen);
   }
}

答案 2 :(得分:0)

正如我所说,有些事情没有加起来。

您似乎使用了不同的主题:

在你的清单中

@style/AppTheme1

,而样式文件包含

@style/AppTheme

答案 3 :(得分:0)

只需将以下代码复制到值-21 &gt;&gt;&gt; Styles.xml 然后清理你的项目并尝试运行。

<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowAnimationStyle">@style/MyAnimation.Window</item>
    <item name="android:windowBackground">@color/background</item>

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

答案 4 :(得分:0)

请参阅此thread

编辑:上述帖子中的一些答案可能会帮助您解决此问题。我将列出一个摘要:

  1. molokoka的回答,为了在您的活动或应用程序中添加ActionBarCompat,应该在AndroidManifest.xml中使用@ style / Theme.AppCompat主题
  2. <activity ... android:theme="@style/Theme.AppCompat" />

    molokoka还指出,通常你需要自定义你的ActionBar。请再次关注他的answer了解详情。

    1. tyczj也提出了一个非常好的观点: 检查并确保您没有引用theme.styled的其他值文件夹,并且不使用AppCompat主题
    2. values-v11文件夹。

      旁注:抱歉,由于声誉不佳,我无法添加评论。这就是我给出答案的原因。

相关问题