我正在尝试将元素添加到已经在片段的onCreateView()方法中成功膨胀的LinearLayout UI中。
下面是onActivityCreated()方法,我正在尝试向UI添加更多元素。
UI为卷计算提供输入,但我不知道在运行时计算卷需要多少维度。维度数量由我的VolumeCalculation类提供,具体取决于 calcId 。
在调试时,只要我尝试在 dimHLayouts [dimNumber] 上设置任何内容,应用就会在 for 循环中崩溃(例如 dimHLayouts [dimNumber] .setId(100 + dimNumber); )。我已经尝试过交换我先设置的属性。一旦我尝试设置属性,无论我首先尝试设置哪个属性,都会发生此崩溃。
我还使用了 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);
}
答案 0 :(得分:0)
我有解决问题的方法(感谢Boris Mocialov的帮助)。
新的UI组件数组已实例化,但数组的各个元素未初始化。
我已经更改 for 循环,如下所示,初始化每个元素,例如dimHLayouts[dimNumber] = new LinearLayout(getActivity());
它现在可以正常工作。
for(int dimNumber=0; dimNumber<calcDimensionCount; dimNumber++) {
// Setup the horizontal linear layout for the dimension
dimHLayouts[dimNumber] = new LinearLayout(getActivity());
dimHLayouts[dimNumber].setOrientation(android.widget.LinearLayout.HORIZONTAL);
dimHLayouts[dimNumber].setLayoutParams(lpDimHLayouts);
// Setup the dimension label TextView
dimTextViews[dimNumber] = new TextView(getActivity());
textViewIds[dimNumber] = generateViewId();
dimTextViews[dimNumber].setId(textViewIds[dimNumber]);
dimTextViews[dimNumber].setLayoutParams(lpDimTextViews);
dimTextViews[dimNumber].setGravity(Gravity.RIGHT);
dimTextViews[dimNumber].setTextAppearance(getActivity(), android.R.attr.textAppearanceMedium);
dimTextViews[dimNumber].setText(calc.getDimLabel(calcId, dimNumber));
// Setup the dimension label EditText
dimEditTexts[dimNumber] = new EditText(getActivity());
editTextIds[dimNumber] = generateViewId();
dimEditTexts[dimNumber].setId(editTextIds[dimNumber]);
dimEditTexts[dimNumber].setLayoutParams(lpDimEditTexts);
dimEditTexts[dimNumber].setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_CLASS_NUMBER);
dimEditTexts[dimNumber].setTextAppearance(getActivity(), android.R.attr.textAppearanceMediumInverse);
// Add TextView and EditText to LinearLayout
dimHLayouts[dimNumber].addView(dimTextViews[dimNumber]);
dimHLayouts[dimNumber].addView(dimEditTexts[dimNumber]);
// Add dimension LinearLayout to the UI
inputUILayout.addView(dimHLayouts[dimNumber]);
}
有没有人知道,是否有更有效和优雅的方法来动态添加一些在运行时未知的元素?