2017-09-01 68 views
0

就像標題所說,我不斷收到錯誤。收到錯誤「您需要使用Theme.AppCompat主題(或後代)這個」 <code>You need to use a Theme.AppCompat theme (or descendant) with this</code>即使<strong>我使用</strong><code>Theme.AppCompat</code>即使我使用Theme.AppCompat

首先,這裏是得到錯誤的類(注:我標記,其中誤差與評論中出現的線):

public class CustomSimpleCursorAdapter extends SimpleCursorAdapter { 

    private Context context; 
    private Cursor cursor; 
    private MainActivity mainActivity; 

    public CustomSimpleCursorAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to, int flags, MainActivity mainActivity) { 
     super(context, layout, cursor, from, to, flags); 
     this.context = context; 
     this.cursor = cursor; 
     this.mainActivity = mainActivity; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent){ 

     // Grab each row as it is pulled from the db. 
     DatabaseHelper dbHelper = new DatabaseHelper(context); 
     View row = super.getView(position, convertView, parent); 

     cursor.moveToPosition(position); 
     final Integer id = cursor.getInt(cursor.getColumnIndex("_id")); 
     final String personID = cursor.getString(cursor.getColumnIndex("personid")); 

     // Set alternating rows to different colors. 
     if(position % 2 == 0){ 
      row.setBackgroundColor(Color.parseColor(Constants.WHITE)); 
     } else { 
      row.setBackgroundColor(Color.parseColor(Constants.LIGHTGREY)); 
     } 

     // Make the delete button clickable. 
     Button deletePersonButton = (Button)row.findViewById(R.id.remove_person); 
     deletePersonButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       AlertDialog.Builder deleteDialogBuilder = new AlertDialog.Builder(context); 
       deleteDialogBuilder.setTitle("Delete Person " + personID + "?"); 
       deleteDialogBuilder.setMessage("This process is IRREVERSABLE!"); 
       deleteDialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         DatabaseHelper databaseHelper = new DatabaseHelper(context); 
         databaseHelper.deletePerson(personID); 
         mainActivity.dataChanged(); 
         Toast.makeText(context, "Person " + personID + " (ID: " + Integer.toString(id) + ") Deleted", Toast.LENGTH_LONG).show(); 
        } 
       }); 
       deleteDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         dialog.dismiss(); 
        } 
       }); 
       AlertDialog deleteDialog = deleteDialogBuilder.create(); 
       deleteDialog.setIcon(R.drawable.warning); 
       deleteDialog.show(); // This is where I get the error 
      } 
     }); 

     return row; 
    } 
} 

這裏就是我所說的CustomSimpleCursorAdapter:

public class Home extends Fragment{ 

    private MainActivity mainActivity; 
    private View rootView; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
     rootView = inflater.inflate(R.layout.home, container, false); 
     mainActivity = (MainActivity)getActivity(); 
     return rootView; 
    } 

    @Override 
    public void onViewCreated(View rootView, Bundle savedInstanceState) { 
     super.onViewCreated(rootView, savedInstanceState); 
     drawThePersonView(); 
    } 

    public void drawThePersonView(){ 
     Context context = mainActivity.getApplicationContext(); 
     DatabaseHelper myDBHelper = new DatabaseHelper(context); 
     Cursor personCursor = myDBHelper.getUndeletedCasualtiesCursor(); 
     String[] fromColumns = {"_id","personID","location","status"}; 
     int[] toViews = {R.id.person_number_textview, R.id.person_personID_textview, R.id.person_location_textview, R.id.person_status_textview}; 
     CustomSimpleCursorAdapter mySimpleCursorAdapter = new CustomSimpleCursorAdapter(context, R.layout.person_layout, personCursor, fromColumns, toViews, 0, mainActivity); 

     ListView myListView = (ListView) rootView.findViewById(R.id.person_row); 

     // Draw the list 
     myListView.setAdapter(mySimpleCursorAdapter); 

     myDBHelper.close(); 
    } 
} 

這裏是我的的AndroidManifest.xml文件:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.domain"> 

    <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:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme" 
     android:name="com.domain.MyApplication"> 
     <activity 
      android:name=".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.Form01" 
      android:label="@string/form_01_title" 
      android:theme="@style/AppTheme.NoActionBar" 
      android:windowSoftInputMode="adjustPan|stateHidden" /> 
     <activity 
      android:name=".forms.Form02" 
      android:label="@string/form_02_title" 
      android:theme="@style/AppTheme.NoActionBar" 
      android:windowSoftInputMode="adjustPan|stateHidden" /> 
     <activity 
      android:name=".forms.Form03" 
      android:label="@string/form_03_title" 
      android:theme="@style/AppTheme.NoActionBar" 
      android:windowSoftInputMode="adjustPan|stateHidden" /> 
     <activity 
      android:name=".AddPerson" 
      android:label="@string/title_activity_add_person" 
      android:theme="@style/AppTheme.NoActionBar" /> 
     <activity 
      android:name=".AddItem" 
      android:label="@string/title_activity_add_item" 
      android:theme="@style/AppTheme.NoActionBar" /> 

    </application> 

</manifest> 

在上面的清單我張貼,還有就是Theme.AppCompat沒有提及,但我沒有進行改變之前,我張貼在這裏。我換了一次一個,然後我改變了他們全部從什麼人到Theme.AppCompat,我仍然收到錯誤,即使我使用Theme.AppCompat每一次。

這是錯誤的logcat的:

09-01 13:07:09.457 26231-26231/com.domain E/AndroidRuntime: FATAL EXCEPTION: main 
    Process: com.domain, PID: 26231 
    java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 
     at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:309) 
     at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:278) 
     at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:252) 
     at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:76) 
     at android.support.v7.app.AlertController.installContent(AlertController.java:216) 
     at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:240) 
     at android.app.Dialog.dispatchOnCreate(Dialog.java:373) 
     at android.app.Dialog.show(Dialog.java:274) 
     at com.domain.adapters.CustomSimpleCursorAdapter$1.onClick(CustomSimpleCursorAdapter.java:57) 
     at android.view.View.performClick(View.java:4780) 
     at android.view.View$PerformClick.run(View.java:19866) 
     at android.os.Handler.handleCallback(Handler.java:739) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:135) 
     at android.app.ActivityThread.main(ActivityThread.java:5258) 
     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:903) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

有什麼建議?

謝謝!

+0

你可以發佈你如何創建CustomSimpleCursorAdapter'的'對象,並設置爲ListView的行可以設置主題,以這樣android:theme="@style/Base.Theme.AppCompat.Dialog"對話活動? – chandil03

+0

@ chandil03我已添加您請求的代碼。謝謝。 – Brian

+0

的[你需要使用Theme.AppCompat主題(或後代)與本次活動(https://stackoverflow.com/questions/21814825/you-need-to-use-a-theme-appcompat-theme可能的複製 - 或 - 後裔與此活動) – Ibrahim

回答

1

簡單地改變getApplicationContext()getContext()

public void drawThePersonView(){ 
     Context context = getContext(); 
     ... 
     // your stuff 
     ... 
} 

我還沒有測試過這段代碼。請測試並讓我知道。

更多閱讀Understanding Context In Android Application

我希望它有幫助。

+0

雖然這工作,我一直有問題的情況下,當我調整設備的空值。這就是爲什麼我首先切換到getApplicationContext()。 – Brian

+0

那麼如果你沒有爲你的片段/ Activity使用多個佈局,我會建議在你的活動中添加'android:configChanges =「keyboardHidden | orientation」',並處理在onConfigChanges方向變化時你需要執行的用例)'方法。那麼你的上下文不會是空的。 – chandil03

0

這個問題已經發生了與我 這是因爲與風格的活動使用警報對話建設者不(Theme.AppCompat) 我改變了風格Theme.AppCompat,但仍然沒有工作 我注意到,機器人工作室有問題,當你改變活動的風格,所以我建議您複製Java和XML有關的活動 的代碼,並刪除該活動,然後用相同的Java和XML但Theme.AppCompat 另一種解決方案,它可以工作作風再重新創建它是不是使提醒對話你可以使你想要建立的對話活動 它更可優化,並且在清單文件中讓你作爲一個對話創建活動顯示爲對話

+0

這就是你剛剛設法解決你的問題,只是做任何你可以不知道究竟是什麼問題。你如果想知道真正的問題,請閱讀以上答案。 – chandil03

相關問題