简体   繁体   English

在不同的活动(弹出活动)中将视图添加到线性布局 Android Studio (Java)

[英]Add views to linear layout in a different activity (popup activity) Android Studio (Java)

I have a popup activity that I create in my program that contains a scrollable view with a linear layout.我在我的程序中创建了一个弹出活动,其中包含一个具有线性布局的可滚动视图。

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

I want to populate that scrollable view with TextView objects, but I am unable to correctly interact with the linear layout, as it returns null when trying to get the view by Id我想用 TextView 对象填充该可滚动视图,但我无法与线性布局正确交互,因为它在尝试按 Id 获取视图时返回 null

I have already done the same thing earlier but within the same view, so I am fairly certain my code is correct to add them, it's just due to the interaction between different activities that is causing the issue.我之前已经做过同样的事情,但在同一个视图中,所以我相当确定我的代码添加它们是正确的,这只是由于不同活动之间的交互导致了问题。 Any help would be appreciated.任何帮助,将不胜感激。

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

    }

You don't access the views of one activity from another, ever.您永远无法从另一项活动中访问一项活动的视图。 You can't even rely on the other Activity still existing, the OS may have deleted it as soon as the 2nd activity started.您甚至不能依赖仍然存在的其他活动,操作系统可能在第二个活动开始后立即将其删除。 Instead, you either相反,你要么

1)Return the values from the activity using startActivityForResult 1) 使用 startActivityForResult 从活动中返回值

or或者

2)Alter an in memory data structure reachable by both activities, and int he onResume of the original Activity you refresh your views from that data structure. 2)更改两个活动都可以访问的 memory 数据结构中的一个,并在原始活动的 onResume 中从该数据结构刷新您的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM