为什么我会收到此"You need to use a Theme.AppCompat theme (or descendant) with this activity"错误?



我在使用新活动时遇到了最令人沮丧的问题。我以"正常"方式创建了它:

Right-Click -> New -> Activity -> Empty Activity

但是,当我尝试启动活动时,我收到此烦人的错误:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

我觉得我的问题与StackOverflow上的其他问题不同,因为我已经在StackOverflow上做了一些探索以及一般的谷歌搜索,但是当有人尝试打开对话框时,这些错误似乎弹出,而不是当他们只是尝试启动新活动时。

这是我的活动类查看所有学生.java

public class ViewAllStudents extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_all_students);
    }
}

我在setContentView(R.layout.activity_view_all_students);行上收到错误。

我的布局资源文件activity_view_all_students.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/view_all_students_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <include layout="@layout/student_table_header" />
    <ListView
        android:id="@+id/student_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

我的包含文件student_table_header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:background="@color/DARKOLIVEGREEN"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:id="@+id/student_number_header"
        style="@style/StdTableHeader"
        android:layout_weight="1"
        android:text="@string/student_number_header"/>
    <TextView
        android:id="@+id/student_id_header"
        style="@style/StdTableHeader"
        android:text="@string/student_id_header"/>
    <TextView
        android:id="@+id/student_location_header"
        style="@style/StdTableHeader"
        android:text="@string/student_location_header"/>
    <TextView
        android:id="@+id/student_status_header"
        style="@style/StdTableHeader"
        android:text="@string/student_status_header"/>
    <TextView
        android:id="@+id/student_delete_header"
        style="@style/StdTableHeader"
        android:text="@string/student_injuries_header"/>
    <TextView
        android:id="@+id/student_deleted_header"
        style="@style/StdTableHeader"
        android:text="@string/student_deleted_header"/>
    <TextView
        android:id="@+id/student_last_modified"
        style="@style/StdTableHeader"
        android:text="@string/student_last_modified_header"/>
</LinearLayout>

最后,我的样式资源文件类型.xml

<resources>
    <style name="StdTableHeader">
        <item name="android:textSize">14sp</item>
        <item name="android:paddingLeft">4dp</item>
        <item name="android:paddingRight">4dp</item>
        <item name="android:paddingTop">2dp</item>
        <item name="android:paddingBottom">2dp</item>
        <item name="android:layout_weight">10</item>
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">@color/WHITE</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textAllCaps">true</item>
        <item name="android:gravity">center</item>
    </style>
</resources>

我的安卓清单.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="my.package">
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:supportsRtl="true">
        <activity
            android:name=".main.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="adjustPan|stateHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".forms.formOne"
            android:label="@string/form_one_header"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="adjustPan|stateHidden" />
        <activity
            android:name=".forms.formTwo"
            android:label="@string/form_two_header"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="adjustPan|stateHidden" />
        <activity
            android:name=".forms.formThree"
            android:label="@string/form_three_header"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="adjustPan|stateHidden" />
        <activity
            android:name=".main.AddStudent"
            android:label="@string/title_activity_add_patient"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity android:name=".main.ViewAllStudents"></activity>
    </application>
</manifest>

添加此属性

android:theme="@style/Theme.AppCompat.Light"

在活动标记中。

或者有样式.xml如下所示

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>

并在清单中添加此属性

android:theme="@style/AppTheme"

在应用程序标记中。

好的...要使长答案简短,请检查您的 AndroidManifest.xml 文件。出于某种原因,AndroidStudio忽略了插入关键行:

android:theme="@style/AppTheme.NoActionBar"

AndroidStudio为我创建的所有其他活动插入了这一行,除了这个。

感谢所有花时间提供帮助并容忍我的无知的人。

相关内容

最新更新