androidandroid-alertdialogandroid-tablelayout

In Android, is it possible to return a TableLayout value from a Popup?


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


Solution

  • 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.