在 Android 中,是否可以 return 来自 Popup 的 TableLayout 值?

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

问题: 我在 AlertDialog.show() 上尝试合并和构建 TableLayout 时收到错误消息它。错误是“您需要为此 activity 使用 Theme.AppCompat 主题(或后代)。”

我正在尝试做的事情: 我正在尝试加载 table 作者列表(一些简单的字符串)以允许用户点击TableRow 和 return 用户的 OnClick 选择。

下面我列出了 1) 基于一些示例的代码,2) 我的 xml 布局和调试跟踪。

Class方法:

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方法:

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

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>

错误跟踪:

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[=15=]0(AddNote.java:22)
        at com.mistywillow.researchdb.AddNote.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)

更新 post 以添加一个演示工作示例 - 这“可能”有帮助 - 使用相同 类、活动和此弹出窗口的编辑副本的弹出窗口 XML activity 布局。这在没有扩展 activity 的情况下仍然有效。但我虚心承认,我可能没有完全理解主题activity依赖

问题是您的 TableLayout 有一个主题,并且该主题需要父 Activity 有一个扩展 Theme.AppCompat.

的主题

所以您要么需要 change/update 您的 Activity 的主题来扩展它,要么您需要 change/update MySpinnerTheme 以便它扩展由你 Activity 的主题

解决方案如下,表明 getApplicationContext() 可能是上面 Mike M. 评论中其他地方的原因:

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.