2017-01-09 43 views
-2

我有一個新的活動最令人沮喪的問題。我創造了它在「正常」的方式:爲什麼我收到這個「你需要在這個活動中使用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> 
+1

提示:'擴展AppCompatActivity' –

+0

[您需要在此活動中使用Theme.AppCompat主題(或後代)](http:// stackoverflow。COM /問題/ 21814825 /你,需要使用的-A-主題程序兼容性主題,或後裔與 - 此活動) – mlg

+0

'你需要使用Theme.AppCompat主題(或後代)與本次活動「這樣做。 –

回答

0

好的......簡單地回答一個簡短的問題,檢查一下你的AndroidManifest.xml文件。出於某種原因,AndroidStudio忽略插入臨界線:

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

AndroidStudio已經插入這行的一切,我已經創建了我的其他活動,除了這一個。

感謝所有花時間幫助和忍受我的無知的人。

1

在活動標籤添加這個屬性

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

還是有一個像下面

<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" 

在應用程序代碼的styles.xml。

+0

無我的其他三個活動的有,他們所有的工作,你已經使用MainActivity – Brian

+0

延伸AppcompactActivity所以你應該使用這個 –

+0

@布賴恩你的其他活動有'AppTheme'恰好延長'Theme.AppCompat'。所以你的其他活動不會崩潰。 –

相關問題