[英]Add views to linear layout in a different activity (popup activity) Android Studio (Java)
我在我的程序中创建了一个弹出活动,其中包含一个具有线性布局的可滚动视图。
<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 对象填充该可滚动视图,但我无法与线性布局正确交互,因为它在尝试按 Id 获取视图时返回 null
我之前已经做过同样的事情,但在同一个视图中,所以我相当确定我的代码添加它们是正确的,这只是由于不同活动之间的交互导致了问题。 任何帮助,将不胜感激。
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();
}
});
}
您永远无法从另一项活动中访问一项活动的视图。 您甚至不能依赖仍然存在的其他活动,操作系统可能在第二个活动开始后立即将其删除。 相反,你要么
1) 使用 startActivityForResult 从活动中返回值
或者
2)更改两个活动都可以访问的 memory 数据结构中的一个,并在原始活动的 onResume 中从该数据结构刷新您的视图。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.