2015-10-16 42 views
0

我创建一个应用程序,我收到错误崩溃,我的活动延伸AppCompatActivity和启动

在启动它崩溃,说

产生的原因:java.lang.IllegalStateException:您需要Theme.AppCompat主题(或后代)与此活动一起使用 。

这里是我的活动:

公共类闪屏扩展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:在 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:在 android.os.Looper.loop(Looper.java:137)10-16 15:56:26.135 25342-25342/com.social_infotech.renthouse E/AndroidRuntime:在 android。 E/AndroidRuntime:在java.lang.reflect.Method.invokeNative(原生 方法)10/15 15:56:26.135 25342-25342/com.social_infotech.renthouse E/AndroidRuntime:在java.lang.reflect.Method.invokeNative(原生 方法) 10-16 15:56:26.135 25342-25342/com.social_infotech.renthouse E/AndroidRuntime:在 java.lang.reflect.Method.invoke(M ethod.java:525)10-16 15:56:26.135 25342-25342/com.social_infotech.renthouse E/AndroidRuntime:在 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:在 android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:60) 10-16 15:56:26.135 25342-25342/com.social_infotech.renthouse E/AndroidRuntime :在 com.social_infotech.renthouse.activity.SplashScreen.onC由于您使用NoAcionBar作为你的主题reate(SplashScreen.java:16)

+2

检查清单,确保您的活动使用'@风格/ AppTheme'或'@风格/ AppBaseTheme'。 – Simas

+0

我的清单使用了android:theme =“@ style/AppTheme” –

+0

您是否包含appCompat支持库作为库 –

回答

0

扩展您的活动与FragmentActivity或活动 。

+0

我都试过,但同样的错误.. –

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); 
    } 
} 
+0

同样的错误,没有变化 –

0

正如我所说的,有些东西没有加起来。

你似乎可以用不同的主题:

在清单中你有

@style/AppTheme1 

而样式文件中包含

@style/AppTheme 
+0

是的,我已经发布清单文件第一,由于某种原因,我已经在这两个我使用相同的风格 –

0

只需将下面的代码复制到您的值 - 21>>>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> 
+0

我已经与做检查这..但没有任何成功 –

0

请参阅此thread

编辑:以上线程的几个答案可能会帮助你解决这个问题。我将列出摘要:

  1. molokoka的回答,为了在您的活动或应用程序应该在AndroidManifest使用@风格/ Theme.AppCompat主题添加ActionBarCompat。这样的XML

<activity ... android:theme="@style/Theme.AppCompat" />

molokoka还指出,通常你需要定制你的动作条。请再次按照他的answer了解详情。

  • tyczj也使得一个非常好的问题: 检查并确保你没有其他文件夹值引用theme.styled并且不使用程序兼容性主题
  • ie values-v11文件夹。

    附注:对不起,我无法添加注释由于低信誉。这就是我给出答案的原因。

    +1

    虽然这个链接可能回答这个问题,但最好在这里包含答案的重要部分,并提供供参考的链接。如果链接页面更改,则仅链接答案可能会失效。 –

    +0

    @Airirh Sharma,道歉。我希望编辑不仅可以帮助OP,也可以帮助任何可能需要它的人。 – AuroMetal

    +0

    没有道歉需要。新用户可以。现在很好。 –

    相关问题