1

I'm working on a Dialog and getting this error that I'm not sure how to fix. Of course I've searched for others with the same, but the solution given was to change the parent class of the activity. My parent activity is a pretty complicated form with a lot of other dialogs and I really really don't want to touch changing it's parent class. What I ideally want is to find the TextView causing this and change it.

the error: E/ThemeUtils: View class androidx.appcompat.widget.AppCompatTextView is an AppCompat widget that can only be used with a Theme.AppCompat theme (or descendant).

I've narrowed the problem to a Spinner. Logcat is filled with 7 copies of this error when taping open the Spinner. Note, this spinner has 4 options.

The error does not happen with other spinners either directly on the Activity or on other Dialogs launched from the same Activity. Which makes me think that I should be able to achieve my dream of fixing this with a change to the Dialog I'm currently working on.

The Spinner's logic in the custom dialog fragment class:

private void setUpNavSpinner(View view) {
        typeSpinner = view.findViewById(R.id.nav_spinner);
        List<CharSequence> typeList = new ArrayList<CharSequence>(
                Arrays.asList(getActivity().getResources()
                        .getStringArray(R.array.repeating_task_options_plural_array)));
        ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getActivity(),
                R.layout.row_spinner_simple,
                typeList ) {
            // show impossible options as grey
            @Override
            public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                View mView = super.getDropDownView(position, convertView, parent);
                TextView mTextView = (TextView) mView;
                int type = position + 1; // spinner is 0-based and types are start at 1=daily
                if (isTypeValid(type)) {
                    mTextView.setTextColor(Color.BLACK);
                } else {
                    mTextView.setTextColor(Color.GRAY);
                }
                return mView;
            }
        };

        // Apply the adapter to the spinner
        typeSpinner.setAdapter(adapter);

        // Set initial value to weekly
        typeSpinner.setSelection(getTypeSpinnerIDFromRTType(rt.getType()));

        // Respond to user clicks
        typeSpinner.setOnItemSelectedListener(this);
    }

Spinner's XML

<Spinner
                    android:id="@+id/nav_spinner"
                    android:layout_width="wrap_content"
                    android:layout_weight="1"
                    android:layout_height="50dp"
                    android:background="@drawable/boarder_accent_bg"/>

row_spinner_simple.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="?android:attr/spinnerItemStyle"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"/>

I'm quite lost, so please let me know if I'm not including important info and I will quickly add it. Thanks in advance for any assistance!

Kaitlyn Hanrahan
  • 759
  • 6
  • 22
  • Does your application/activity use `Theme.AppCompat theme (or descendant)` in styles/themes XML file? – Zain May 22 '22 at 11:13
  • You have probably seen [this](https://stackoverflow.com/a/21815015/6287910) Stack Overflow answer. The issue arises with the _TextView_ in _row_spinner_simple.xml_ and the theme you have specified for your app as Zain has said. You will need to supply the spinner with a Theme.AppCompat theme or a descendant as the error states. – Cheticamp May 22 '22 at 13:53

1 Answers1

1

Fixed by updating the spinner row xml file as follows:

<?xml version="1.0" encoding="utf-8"?>
<android.widget.TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="?android:attr/spinnerItemStyle"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"/>

Again, I'm out of my depth with this one, but it seems the original TextView was an androidx.appcompat.widget.AppCompatTextView despite being a "textView" rather than a "AppCompatTextView". The same issue in the "built in" xml layout for simple Spinner rows.

If anyone wants to explain to me why the TextView defaulted to androidx.appcompat.widget.AppCompatTextView rather than android.widget.TextView -- I'm all ears and will give you the Bounty. I'm a little worried I could have other appcombat vs "regular" compatibility issues due to my ignorance.

Kaitlyn Hanrahan
  • 759
  • 6
  • 22
  • The [Appcompat](https://developer.android.com/jetpack/androidx/releases/appcompat) library _"Allows access to new APIs on older API versions of the platform"_ and is, in general, to be preferred. The same code will run on all APIs and should be consistent. What you have specified is the framework version of _TextView_ which may differ from one version of Android to another. I suggest that you use Appcompat if you can. – Cheticamp May 23 '22 at 11:24