【问题标题】:Add views to linear layout in a different activity (popup activity) Android Studio (Java)在不同的活动(弹出活动)Android Studio(Java)中将视图添加到线性布局
【发布时间】:2022-02-01 23:25:15
【问题描述】:

我在我的程序中创建了一个弹出活动,其中包含一个具有线性布局的可滚动视图。

<ScrollView
        android:id="@+id/scrollView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginTop="32dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="32dp"
        app:layout_constraintBottom_toTopOf="@+id/averageRollsText"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/totalRollsText">

        <LinearLayout
            android:id="@+id/histogramLinearLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="2dp"
            android:orientation="vertical" />
    </ScrollView>

我想用 TextView 对象填充该可滚动视图,但我无法与线性布局正确交互, as it returns null when trying to get the view by Id

我之前已经在同一个视图中做过同样的事情,所以我相当确定我的代码添加它们是正确的,这只是由于不同活动之间的交互导致了问题。任何帮助将不胜感激。

public void generateHistogramButton(View view){
        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
        final View histogramPopupView = getLayoutInflater().inflate(R.layout.histogram_popup, null);
        TextView totalRollsText = histogramPopupView.findViewById(R.id.totalRollsText);
        TextView avgText = histogramPopupView.findViewById(R.id.averageRollsText);
        TextView minText = histogramPopupView.findViewById(R.id.minRollText);
        TextView maxText = histogramPopupView.findViewById(R.id.maxRollText);

        totalRollsText.setText("Total Rolls: " + gameHistogram.getTotalRolls());
        avgText.setText("Avg: " + gameHistogram.getAverageRoll());
        minText.setText("Min: " + gameHistogram.getMinRoll());
        maxText.setText("Max: " + gameHistogram.getMaxRoll());

        View histogramLinearLayout = histogramPopupView.findViewById(R.id.histogramLinearLayout);

        int[] totalRolls = gameHistogram.returnRolls();
        int[] histogramValues = gameHistogram.generateHistogram(20);
        String histogramString = "";

        for(int i = 0; i < gameHistogram.getNumRange(); i++){
            TextView newHistText = new TextView(histogramPopupView.getContext());
            newHistText.setSingleLine(true);
            newHistText.setId(100+i);
            newHistText.setText("");
            newHistText.setTextSize(22);
            newHistText.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

            histogramString = "";
            histogramString += (String.format("%2d ",i+gameDice.getMinRoll()) + ": (" + String.format("%2d",totalRolls[i]) + ") ");
            for(int j = 0; j <= histogramValues[i]; j++){
                histogramString += "#";
            }
            histogramString += "\n";
            newHistText.setText(histogramString);
            ((LinearLayout) histogramLinearLayout).addView(newHistText);
        }


        Button exitHistogramButton = histogramPopupView.findViewById(R.id.closeHistogramButton);

        dialogBuilder.setView(histogramPopupView);
        Dialog dialog = dialogBuilder.create();
        dialog.show();

        exitHistogramButton.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                dialog.dismiss();
            }
        });

    }

【问题讨论】:

    标签: java android android-studio


    【解决方案1】:

    您永远无法从另一个活动访问一个活动的视图。您甚至不能依赖仍然存在的其他活动,操作系统可能在第二个活动开始后立即将其删除。相反,你要么

    1)使用 startActivityForResult 从活动中返回值

    2) 更改两个 Activity 都可以访问的内存数据结构,然后在原始 Activity 的 onResume 中刷新该数据结构的视图。

    【讨论】:

    • 我刚刚意识到我错过了这个问题的一个重要部分。我正在使用 layoutInflater 来创建弹出视图,因此它们都同时存在 afaik。它不再崩溃,但它只是显示为空。我将编辑它以获得我的完整代码,包括充气机的东西
    猜你喜欢
    • 1970-01-01
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-17
    相关资源
    最近更新 更多