我尝试使用Material Design方法更改颜色。 但在此之后,当我想运行我的应用程序时,它已停止工作。 当我点击应用程序图标运行这个时,我只看到这个应用程序大约1秒钟。 我看到颜色已更改,但此应用程序停止正常工作。 我不知道问题出在哪里,因为我在Android官方网站上使用说明。
有人可以帮我解决这个问题吗?
以下是我的代码:
Styles.xml:
<!-- Base application theme. -->
<style name="AppTheme" parent="android:Theme.Material">
<!-- Customize your theme here. -->
<item name="android:colorPrimary">@color/primaryColor</item>
<item name="android:colorPrimaryDark">@color/colorprimarydark</item>
<item name="android:windowBackground">@color/windowbackground</item>
<item name="android:textColorPrimary">@color/textcolorprimary</item>
</style>
Colors.xml:
<color name="primaryColor">#FF9800</color>
<color name="colorprimarydark">#FF3F00</color>
<color name="windowbackground">#BDBDBD</color>
<color name="textcolorprimary">#039BE5</color>
Activity.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.karol.material_test.MainActivity">
<TextView
android:text="Hello World!"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
logcat的:
01-20 21:27:12.120 28957-28957/com.example.karol.material_test E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.karol.material_test, PID: 28957
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.karol.material_test/com.example.karol.material_test.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2406)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2466)
at android.app.ActivityThread.access$1200(ActivityThread.java:152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5538)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:151)
at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
at com.example.karol.material_test.MainActivity.onCreate(MainActivity.java:12)
at android.app.Activity.performCreate(Activity.java:6013)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2359)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2466)
at android.app.ActivityThread.access$1200(ActivityThread.java:152)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1341)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5538)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:960)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
01-20 21:27:12.510 28957-28957/com.example.karol.material_test I/Process: Sending signal. PID: 28957 SIG: 9
答案 0 :(得分:0)
您需要使用
Theme.AppCompat
主题(或后代) 活性。
而不是:android:Theme.Material
检查一下:
https://stackoverflow.com/a/21815015/4409113
只需在样式中使用AppCompat
或在Java代码中使用AppCompatActivity
。