0

Problem: I'm receiving a error on .show() of my AlertDialog when trying to incorporate and build out a TableLayout within it. The error is "You need to use a Theme.AppCompat theme (or descendant) with this activity."

What I'm trying to do: I'm trying to load a table list of authors (some simple strings) to allow the user to click on the TableRow and return the user's OnClick choice.

Below I've listed 1) my code based upon some samples, 2) my xml layout and the debug trace.

Class Method:

public static String AlertSelectAuthor(Context context, String title, String message, List<Sources> sources){
    // Create a AlertDialog Builder.
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
    // Set title, icon, can not cancel properties.
    alertDialogBuilder.setTitle(title);
    alertDialogBuilder.setIcon(R.drawable.ic_launcher_background);
    alertDialogBuilder.setCancelable(false);

    // Set the inflated layout view object to the AlertDialog builder.
    View popup = SelectAuthorTable(context, message, sources);
    alertDialogBuilder.setView(popup);

    // Create AlertDialog and show.
    final AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
    TableLayout input = popup.findViewById(R.id.authorTblLayout);
    return "Return OnClick choice";
}

Class Method:

private static View SelectAuthorTable(Context context, String message, List<Sources> sources){
    // Get layout inflater object.
    LayoutInflater layoutInflater = LayoutInflater.from(context);
    View popupSourcesDialog = layoutInflater.inflate(R.layout.popup_sources_dialog, null);
    TableLayout tableLayout = popupSourcesDialog.findViewById(R.id.authorTblLayout);
    for(Sources source : sources){
        String author = DBQueryTools.concatenateAuthors(DBQueryTools.getAuthorsBySource(source));
        TableRow row = new TableRow(context);
        row.addView(BuildTableLayout.addRowTextViewToTable(context, author, false));
        row.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, row.getChildAt(0).toString(), Toast.LENGTH_LONG).show();
            }
        });
        tableLayout.addView(row);
    }

The XML:

<?xml version="1.0" encoding="utf-8"?>
    
        <androidx.constraintlayout.widget.ConstraintLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        xmlns:app="http://schemas.android.com/apk/res-auto">

            <TableLayout
                    android:id="@+id/authorTblLayout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:shrinkColumns="0"
                    android:stretchColumns="1"
                    android:background="@color/colorDkGray"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintTop_toBottomOf="parent"
                    android:padding="2dp"
                    android:layout_marginTop="15dp"
                    android:layout_marginEnd="5dp"
                    android:layout_marginStart="5dp">
        
            </TableLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

The Error Trace:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.mistywillow.researchdb, PID: 11198
    java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
        at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:846)
        at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:809)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:696)
        at androidx.appcompat.app.AppCompatDialog.setContentView(AppCompatDialog.java:95)
        at androidx.appcompat.app.AlertController.installContent(AlertController.java:232)
        at androidx.appcompat.app.AlertDialog.onCreate(AlertDialog.java:279)
        at android.app.Dialog.dispatchOnCreate(Dialog.java:407)
        at android.app.Dialog.show(Dialog.java:302)
        at com.mistywillow.researchdb.PopupDialog.AlertSelectAuthor(PopupDialog.java:116)
        at com.mistywillow.researchdb.DBQueryTools.captureAuthorNewOrOldSource(DBQueryTools.java:105)
        at com.mistywillow.researchdb.AddNote.populateSourceDetails(AddNote.java:297)
        at com.mistywillow.researchdb.AddNote.access$000(AddNote.java:22)
        at com.mistywillow.researchdb.AddNote$5.onLayoutChange(AddNote.java:155)
        at android.view.View.layout(View.java:20690)
        at android.view.ViewGroup.layout(ViewGroup.java:6194)
        at androidx.constraintlayout.widget.ConstraintLayout.onLayout(ConstraintLayout.java:1855)

Updated post to add a demonstrating working example - that "might" be helpful - of a popup that is using the same classes, activities and an edited copy of this popups XML activity layout. This was and is working without the extended activity. But I humbly admit, I may not fully understand the theme activity dependency.

Popup Example

svstackoverflow
  • 665
  • 6
  • 19
  • 1
    Does this answer your question? [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) – javdromero Aug 12 '21 at 21:51
  • 1
    Whatever `Context` you're passing to your method does not have an appropriate theme on it. The most common cause we see for that here is using `getApplicationContext()` instead of the current `Activity`, but we don't really know how or where you're calling this. – Mike M. Aug 13 '21 at 17:16
  • @Mike M. -- That was it! There was a use of `getApplicationContext`, and when I updated it to the `[class].this`, it worked. – svstackoverflow Aug 16 '21 at 12:02
  • @Mike M. -- So you get credit for the answer, could you copy and paste your comment as an answer so I can mark it as the answer... which was changing the `getApplicationContext` to a `[class].this`. – svstackoverflow Aug 17 '21 at 10:50
  • Oh, I'm good. :-) Nothin' huge. Thank you, though. I really appreciate the offer. Glad you got it working. Cheers! – Mike M. Aug 17 '21 at 15:41

2 Answers2

0

The issue is that your TableLayout has a theme and that theme requires the parent Activity to have a theme that extends Theme.AppCompat.

So you either need to change/update your Activity's theme to extend that or you need to change/update MySpinnerTheme so that it extends a theme that's supported by your Activity's theme

Nathan K
  • 199
  • 7
  • For others in the future as well as my clarification - do you mean I need to Extend the Popup class I created to handle popups with `AppCompatActivity`? – svstackoverflow Aug 13 '21 at 11:55
  • I have tried extending `AppCompatActivity`, including `Activity` using the link in javdromero's posted above, but neither worked. Interestingly, I have two other Popups that work just fine created just like above - without any of these theme updates suggested. So I'm not sure what is going on exactly. I have no doubt I need some theme fix, but not even my other two XML popup activities have themes assigned and they work just fine. So Just adding some followup details. Thanks for the help. – svstackoverflow Aug 13 '21 at 12:11
  • Look at your AndroidManifest.xml file. There should be a theme loaded on the `` (looks something like: `android:theme="@style/WhateverNameAppTheme"`). You can also set themes on Activities as well, but you likely don't have that. So whatever that theme is there? That's the theme of your activity. It needs to extend `Theme.AppCompat` – Nathan K Aug 13 '21 at 17:51
  • Within the context of this post, the solution pertained to Mike M's comment above, which the use of `getApplicationContext` was the cause. – svstackoverflow Aug 16 '21 at 12:04
0

The solution was the following, suggesting that getApplicationContext() may be a cause elsewhere in a comment by Mike M. above:

Whatever Context you're passing to your method does not have an appropriate theme on it. The most common cause we see for that here is using getApplicationContext() instead of the current Activity, but we don't really know how or where you're calling this.

svstackoverflow
  • 665
  • 6
  • 19