我正在尝试将元素添加到已在片段的 onCreateView() 方法中成功膨胀的 LinearLayout UI。
下面是我尝试向 UI 添加更多元素的 onActivityCreated() 方法。
UI 为体积计算提供输入,但我不知道在运行时需要多少维度来计算体积。我的 VolumeCalculation 类根据 calcId 提供维数。
在调试时,只要我尝试在dimHLayouts[dimNumber]上设置任何内容(例如dimHLayouts[dimNumber].setId(100 + dimNumber); ),应用程序就会在for循环中崩溃。我试过交换我首先设置的属性。无论我首先尝试设置哪个属性,只要我尝试设置属性,就会发生此崩溃。
我还使用了android.util.Log工具并插入了大量日志来尝试查看发生了什么,但我仍然不知所措,因为到目前为止一切似乎都按预期工作。任何帮助将不胜感激。
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// Get the volume Calculation Type from the bundle passed to this fragment
Bundle bundle = this.getArguments();
int calcId = bundle.getInt("CalcType");
// Create a calculation object
VolumeCalculation calc = new VolumeCalculation(ApplicationContextProvider.getContext());
int calcDimensionCount = calc.getCalcDimensionCount(calcId);
// Get the root LinearLayout of the inflated UI
LinearLayout inputUILayout = (LinearLayout)view.findViewById(R.id.layout_input);
// Create array of new LinearLayout objects
LinearLayout[] dimHLayouts = new LinearLayout[calc.getCalcDimensionCount(calcId)];
LinearLayout.LayoutParams lpDimHLayouts = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
// Create array of new TextView objects
TextView[] dimTextViews = new TextView[calc.getCalcDimensionCount(calcId)];
LayoutParams lpDimTextViews = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
for(int dimNumber=0; dimNumber<calcDimensionCount; dimNumber++) {
// Setup the horizontal linear layout for the dimension
dimHLayouts[dimNumber].setId(100 + dimNumber);
dimHLayouts[dimNumber].setOrientation(android.widget.LinearLayout.HORIZONTAL);
dimHLayouts[dimNumber].setLayoutParams(lpDimHLayouts);
// Setup the dimension label TextView
dimTextViews[dimNumber].setId(150 + dimNumber);
dimTextViews[dimNumber].setLayoutParams(lpDimTextViews);
dimTextViews[dimNumber].setGravity(Gravity.RIGHT);
dimTextViews[dimNumber].setText(calc.getDimLabel(calcId, dimNumber));
// Add TextView to LinearLayout
dimHLayouts[dimNumber].addView(dimTextViews[dimNumber]);
// To Do: Setup an EditText and add it to the dimension LinearLayout
// Add dimension LinearLayout to the UI
inputUILayout.addView(dimHLayouts[dimNumber]);
}
super.onActivityCreated(savedInstanceState);
}