ArrayList的Android内存不足异常<;斯汀>;
我的代码运行良好,然后我设置了一些if-else条件。 我认为for循环中存在优化问题,但没有得到它ArrayList的Android内存不足异常<;斯汀>;,android,performance,android-activity,arraylist,out-of-memory,Android,Performance,Android Activity,Arraylist,Out Of Memory,我的代码运行良好,然后我设置了一些if-else条件。 我认为for循环中存在优化问题,但没有得到它 btSetQuantity.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (etbillQuantity.getText().toString().equals("")) {
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();
}
}
});
看这里,由于您的逻辑错误,外部循环已成为无限循环
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());
}
}
}
else{
//你的其他代码
对于(int i=0;i<(tempproduct_Code.size());i++){
对于(int j=0;j<(GlobalVariables.arrayOfTempProductCode.size());j++){
if(tempproduct_Code.get(i).contains(GlobalVariables.arrayOfTempProductCode
.get(j))){
//你的其他代码
}否则{
tempproduct_Code.add(tvCode.getText().toString());//在这里,您不断增加tempproduct_代码的大小,并且外部循环的条件也取决于相同的大小。因此,您的循环正在运行&最终导致OutOfMemory错误
tempproduct_Name.add(tvName.getText().toString());
临时产品数量。添加(强化数量);
tempproduct_UnitPrice.add(tvPrice.getText().toString());
}
}
}
因此,您的代码变得像狗在追逐自己的尾巴。检查您的业务需求并修改您的逻辑。请参见此处,由于您的逻辑错误,外部循环已成为无限循环
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());
}
}
}
else{
//你的其他代码
对于(int i=0;i<(tempproduct_Code.size());i++){
对于(int j=0;j<(GlobalVariables.arrayOfTempProductCode.size());j++){
if(tempproduct_Code.get(i).contains(GlobalVariables.arrayOfTempProductCode
.get(j))){
//你的其他代码
}否则{
tempproduct_Code.add(tvCode.getText().toString());//在这里,您不断增加tempproduct_代码的大小,并且外部循环的条件也取决于相同的大小。因此,您的循环正在运行&最终导致OutOfMemory错误
tempproduct_Name.add(tvName.getText().toString());
临时产品数量。添加(强化数量);
tempproduct_UnitPrice.add(tvPrice.getText().toString());
}
}
}
因此,您的代码变得像狗在追逐自己的尾巴。检查您的业务需求并修改您的逻辑。请参见此处,由于您的逻辑错误,外部循环已成为无限循环
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());
}
}
}
else{
//你的其他代码
对于(int i=0;i<(tempproduct_Code.size());i++){
对于(int j=0;j<(GlobalVariables.arrayOfTempProductCode.size());j++){
if(tempproduct_Code.get(i).contains(GlobalVariables.arrayOfTempProductCode
.get(j))){
//你的其他代码
}否则{
tempproduct_Code.add(tvCode.getText().toString());//在这里,您不断增加tempproduct_代码的大小,并且外部循环的条件也取决于相同的大小。因此,您的循环正在运行&最终导致OutOfMemory错误
tempproduct_Name.add(tvName.getText().toString());
临时产品数量。添加(强化数量);
tempproduct_UnitPrice.add(tvPrice.getText().toString());
}
}
}
因此,您的代码变得像狗在追逐自己的尾巴。检查您的业务需求并修改您的逻辑。请参见此处,由于您的逻辑错误,外部循环已成为无限循环
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());
}
}
}
else{
//你的其他代码
对于(int i=0;i<(tempproduct_Code.size());i++){
对于(int j=0;j<(GlobalVariables.arrayOfTempProductCode.size());j++){
if(tempproduct_Code.get(i).contains(GlobalVariables.arrayOfTempProductCode
.get(j))){
//你的其他代码
}否则{
tempproduct_Code.add(tvCode.getText().toString());//在这里,您不断增加tempproduct_代码的大小,并且外部循环的条件也取决于相同的大小。因此,您的循环正在运行&最终导致OutOfMemory错误
tempproduct_Name.add(tvName.getText().toString());
临时产品数量。添加(强化数量);
tempproduct_UnitPrice.add(tvPrice.getText().toString());
}
}
}
因此,您的代码变得像狗在追逐自己的尾巴。检查您的业务需求并修改您的逻辑。@Skynet完成请检查第349行是什么,请在此处标记它?错误是否总是出现在这一行?@Skynet tempproduct_code.add(tvCode.getText().toString());不知道如何标记请帮助如何定义
tempproduct\u code
@Skynet private ArrayList tempproduct\u code=new ArrayList();@Skynet done请检查第349行是什么,请在这里标记它?错误是否总是发生在这一行?@Skynet tempproduct\u code.add(tvCode.getText().toString());不知道如何标记请帮助如何定义tempproduct\u code
@Skynet private ArrayList tempproduct\u code=new ArrayList();@Skynet done请检查第349行是什么,请在这里标记它?错误是否总是发生在这一行?@Skynet tempproduct\u code.add(tvCode.getText().toString());不知道如何标记请帮助如何定义tempproduct\u code
@Skynet专用阵列