我有一個新的活動最令人沮喪的問題。我創造了它在「正常」的方式:爲什麼我收到這個「你需要在這個活動中使用Theme.AppCompat主題(或後代)」錯誤?
Right-Click -> New -> Activity -> Empty Activity
,當我嘗試啓動活動不過我得到這個惱人的錯誤:
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
我覺得我的問題是從StackOverflow上和別人不同因爲我已經在StackOverflow上做了一些探索,並且在一般情況下使用了谷歌搜索,但是當有人試圖打開一個對話框時,這些錯誤似乎會彈出,而不是他們只是試圖啓動一個新的活動。
這裏是我的活動課ViewAllStudents.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>
最後,我的風格的資源文件styles.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>
我AndroidManifest.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>
提示:'擴展AppCompatActivity' –
[您需要在此活動中使用Theme.AppCompat主題(或後代)](http:// stackoverflow。COM /問題/ 21814825 /你,需要使用的-A-主題程序兼容性主題,或後裔與 - 此活動) – mlg
'你需要使用Theme.AppCompat主題(或後代)與本次活動「這樣做。 –