1

我正在尝试将元素添加到已在片段的 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);
}
4

1 回答 1

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]);
    }

有谁知道,有没有更有效和优雅的方法来动态添加一些在运行时未知的元素?

于 2013-08-03T22:54:59.893 回答