收到错误“您需要使用Theme.AppCompat主题(或后代)这个” You need to use a Theme.AppCompat theme (or descendant) with this即使我使用Theme.AppCompat即使我使用Theme.AppCompat

问题描述:

就像标题所说,我不断收到错误。收到错误“您需要使用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

简单地改变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

这个问题已经发生了与我 这是因为与风格的活动使用警报对话建设者不(Theme.AppCompat) 我改变了风格Theme.AppCompat,但仍然没有工作 我注意到,机器人工作室有问题,当你改变活动的风格,所以我建议您复制Java和XML有关的活动 的代码,并删除该活动,然后用相同的Java和XML但Theme.AppCompat 另一种解决方案,它可以工作作风再重新创建它是不是使提醒对话你可以使你想要建立的对话活动 它更可优化,并且在清单文件中让你作为一个对话创建活动显示为对话

+0

这就是你刚刚设法解决你的问题,只是做任何你可以不知道究竟是什么问题。你如果想知道真正的问题,请阅读以上答案。 – chandil03