ArrayList <sting>的Android-Out of Memory异常

时间:2015-04-24 13:31:19

标签: android performance android-activity arraylist out-of-memory

在我放入一些if else条件之前,我的代码运行正常。 我认为for循环中存在优化问题,但没有得到它

 var array = [0,1,2,3,4,5]
 var removeItem = 3;

 array = jQuery.grep(array, function(value) {
    return value != removeItem;
 });

它现在在日志中显示Out of Memory 我已经使用最佳方式进行列表视图性能优化 日志显示arraylist内存不足 日志还显示增长堆一段时间app是struct和crashes

     btSetQuantity.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {

                        if (etbillQuantity.getText().toString().equals("")) {
                            Toast.makeText(getApplicationContext(),
                                    "Quantity Not Entered", Toast.LENGTH_SHORT)
                                    .show();
                            InputMethodManager imm = (InputMethodManager) getSystemService(context.INPUT_METHOD_SERVICE);
                            imm.hideSoftInputFromWindow(
                                    etbillQuantity.getWindowToken(), 0);
                            alertDialog.dismiss();
                        } else if (Double.parseDouble(etbillQuantity.getText()
                                .toString()) > initialQuantityValue) {
                            Toast.makeText(getApplicationContext(),
                                    "Short Of Quantity", Toast.LENGTH_SHORT)
                                    .show();

                        } else {

                            strEnteredQuantity = etbillQuantity.getText()
                                    .toString();

                            enteredQuantityValue = Double
                                    .parseDouble(strEnteredQuantity);

                            tempproduct_TotalQuantity.add(tvInitialQuantity
                                    .getText().toString());

                            newQuantityValue = initialQuantityValue
                                    - enteredQuantityValue;

                            setQuantity.setText(strEnteredQuantity);
                            String setQuantityStr = "";

                            GlobalVariables.arrayOfTempProductSetQuantity
                                    .add(strEnteredQuantity);
                            GlobalVariables.arrayOfTempProductName.add(tvName
                                    .getText().toString());
                            GlobalVariables.arrayOfTempProductCode.add(tvCode
                                    .getText().toString());
                            GlobalVariables.arrayOfTempProductUnitPrice
                                    .add(tvPrice.getText().toString());
                            GlobalVariables.arrayOfTempProductQuantity
                                    .add((String.valueOf(newQuantityValue)));

                            if (tempproduct_Code.size() <= 0) {
                                tempproduct_Code.add(tvCode.getText()
                                        .toString());
                                tempproduct_Name.add(tvName.getText()
                                        .toString());
                                tempproduct_Quantity.add(strEnteredQuantity);
                                tempproduct_UnitPrice.add(tvPrice.getText()
                                        .toString());
                            }

                            else {
                                String setQuantityTemp = "";
                                String setUnitPriceTemp = "";
                                String setNameTemp = "";
                                String setCodeTemp = "";

                                for (int i = 0; i < (tempproduct_Code.size()); i++) {
                                    for (int j = 0; j < (GlobalVariables.arrayOfTempProductCode
                                            .size()); j++) {

                                        if (tempproduct_Code
                                                .get(i)
                                                .contains(
                                                        GlobalVariables.arrayOfTempProductCode
                                                                .get(j))) {
                                            setQuantityTemp = GlobalVariables.arrayOfTempProductSetQuantity
                                                    .get(j);
                                            tempproduct_Quantity.set(i,
                                                    setQuantityTemp);

                                            setCodeTemp = GlobalVariables.arrayOfTempProductCode
                                                    .get(j);
                                            tempproduct_Code
                                                    .set(i, setCodeTemp);
                                            setNameTemp = GlobalVariables.arrayOfTempProductName
                                                    .get(j);
                                            tempproduct_Name
                                                    .set(i, setNameTemp);
                                            setUnitPriceTemp = GlobalVariables.arrayOfTempProductUnitPrice
                                                    .get(j);
                                            tempproduct_UnitPrice.set(i,
                                                    setUnitPriceTemp);

                                        } else {
                                            tempproduct_Code.add(tvCode
                                                    .getText().toString());
                                            tempproduct_Name.add(tvName
                                                    .getText().toString());
                                            tempproduct_Quantity
                                                    .add(strEnteredQuantity);
                                            tempproduct_UnitPrice.add(tvPrice
                                                    .getText().toString());
                                        }
                                    }
                                }
                            }
                            InputMethodManager imm = (InputMethodManager) getSystemService(context.INPUT_METHOD_SERVICE);
                            imm.hideSoftInputFromWindow(
                                    etbillQuantity.getWindowToken(), 0);
                            onSetQuantityDisplayData();
                            alertDialog.dismiss();

                        }
                    }
                    });

1 个答案:

答案 0 :(得分:2)

看到这里,由于你的逻辑错误,外部循环已成为一个无限循环。

  else {
// YOUR OTHER CODES

        for (int i = 0; i < (tempproduct_Code.size()); i++) {
           for (int j = 0; j < (GlobalVariables.arrayOfTempProductCode                                                    .size()); j++) {

             if (tempproduct_Code.get(i).contains(GlobalVariables.arrayOfTempProductCode
                                                                    .get(j))) {
                  // your other codes

               } else {
                   tempproduct_Code.add(tvCode.getText().toString()); // here you are continuously increasing your tempproduct_Code's size and your outer loops condition is also depending on the same size. thus your loop is running & ultimately leading you to OutOfMemory error
                   tempproduct_Name.add(tvName.getText().toString());
                   tempproduct_Quantity.add(strEnteredQuantity);
                   tempproduct_UnitPrice.add(tvPrice.getText().toString());
              }
       }
 }

因此,您的代码变得像狗正在追逐它自己的尾巴。检查您的业务需求&amp;修改你的逻辑