【发布时间】: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