简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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