0

Like the title says, I keep getting the error You need to use a Theme.AppCompat theme (or descendant) with this even though I am using Theme.AppCompat.

First, Here is the class that is getting the error (NOTE: I marked the line where the error is occurring with a comment):

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;
    }
}

Here is where I call 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();
    }
}

And here is my AndroidManifest.xml file:

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

In the above manifest I posted, there is no reference to Theme.AppCompat, but I did make that change before I posted here. I changed one at a time, then I changed them all from what they were to Theme.AppCompat and I still received the error every single time even though I am using Theme.AppCompat.

This is the logcat of the error:

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)

Any suggestions?

Thanks!

Brian
  • 1,726
  • 2
  • 24
  • 62
  • can you post the line how you are creating object of `CustomSimpleCursorAdapter` and setting to listview? – Sachin Chandil Sep 01 '17 at 18:13
  • @chandil03 I have added the code you requested. Thanks. – Brian Sep 01 '17 at 18:24
  • Possible duplicate of [You need to use a Theme.AppCompat theme (or descendant) with this activity](https://stackoverflow.com/questions/21814825/you-need-to-use-a-theme-appcompat-theme-or-descendant-with-this-activity) –  Sep 01 '17 at 18:26

2 Answers2

1

Simply change the getApplicationContext() to getContext().

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

I haven't tested this code. Please test and let me know.

For more read Understanding Context In Android Application.

I hope it helps.

Sachin Chandil
  • 17,133
  • 8
  • 47
  • 65
  • Although this works, I have been having issues with the context being null when I reorient the device. That is why I switched to getApplicationContext() in the first place. – Brian Sep 01 '17 at 18:59
  • Well if you are not using multiple layouts for your fragment/Activity, i would suggest adding `android:configChanges="keyboardHidden|orientation"` in your activity and handle if you have a use case to perform when orientation changes in `onConfigChanges()` method. Then your context wont be null. – Sachin Chandil Sep 01 '17 at 19:34
0

This problem already happened with me It was because of using Alert dialogue builder in an activity with a style not (Theme.AppCompat) I changed the style to Theme.AppCompat but still not working I noticed that android studio have problems when you change the style of an activity so I recommend you copy the code of java and xml related to your activity and delete the activity then recreate it again with the same java and xml but a style of Theme.AppCompat Another solution that may work is that instead of making the alert dialogue you may make an activity of the dialogue you want to build it is more custimizable and in the manifest file you can set the theme to the dialogue activity like this android:theme="@style/Base.Theme.AppCompat.Dialog" so that the activity you created as a dialogue is displayed as a dialogue

Ahmed Rajab
  • 603
  • 3
  • 10
  • 28
  • This is what you just managed to solve your issue by just doing whatever you could without knowing what exactly problem was. You if want to know the real issue read above answer. – Sachin Chandil Sep 01 '17 at 19:37