错误:
You need to use a Theme.AppCompat theme (or descendant) with this activity
意味着您需要使用派生自 AppCompat 主题的活动样式。它通常在res/values/styles.xml
. 如果你没有创建它是这样的:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
然后,通过将其添加到 AndroidManifest.xml 将其用于您的活动android:theme="@style/AppTheme"
(请阅读评论):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sample.testcode">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<!-- Or you can use specific theme for the activity -->
<activity
android:name=".anotherActivity"
android:label="@string/app_name" >
android:theme="@style/AppTheme" >
</activity>
</application>
</manifest>