java.lang.IllegalStateException:YouneedtouseaTheme.AppCompattheme

developerguy發表於2015-09-20

配置:

中設定theme為

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"

        android:theme="@android:style/Theme.Light.NoTitleBar"
        android:label="@string/app_name"
        >

報錯資訊:

09-20 10:31:58.185    1392-1392/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: networkimageviewer.com.myapplication, PID: 1392
    java.lang.RuntimeException: Unable to start activity ComponentInfo{networkimageviewer.com.myapplication/networkimageviewer.com.myapplication.WelcomeActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2192)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2241)
            at android.app.ActivityThread.access$800(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5049)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
            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.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:309)
            at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:278)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:252)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:109)
            at networkimageviewer.com.myapplication.WelcomeActivity.onCreate(WelcomeActivity.java:22)
            at android.app.Activity.performCreate(Activity.java:5249)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2156)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2241)
            at android.app.ActivityThread.access$800(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1204)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5049)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
            at dalvik.system.NativeStart.main(Native Method)

解決辦法:
To simply add ActionBar Compat your activity or application should use @style/Theme.AppCompat theme in AndroidManifest.xml like this:

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"

        android:theme="@style/Theme.AppCompat.Light.NoActionBar"
        android:label="@string/app_name"
        >

 

http://stackoverflow.com/questions/18063395/actionbarcompat-java-lang-illegalstateexception-you-need-to-use-a-theme-appcom