【发布时间】:2015-04-24 13:31:18
【问题描述】:
在我设置一些 if else 条件之前,我的代码运行良好。 我认为 for 循环中存在优化问题,但没有得到它
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();
}
}
});
现在在日志中显示内存不足 我已经使用了列表视图性能优化的最佳方法 显示数组列表内存不足的日志 日志还显示了一段时间的增长堆应用程序是结构和崩溃
04-24 12:31:27.463: I/dalvikvm-heap(1136): Grow heap (frag case) to 20.725MB for 4021256-byte allocation
04-24 12:31:28.063: D/dalvikvm(1136): GC_FOR_ALLOC freed 2618K, 34% free 18530K/27816K, paused 536ms, total 536ms
04-24 12:31:28.063: I/dalvikvm-heap(1136): Grow heap (frag case) to 22.003MB for 4021256-byte allocation
04-24 12:31:37.813: D/dalvikvm(1136): GC_FOR_ALLOC freed 2618K, 29% free 19839K/27816K, paused 708ms, total 708ms
04-24 12:31:37.813: I/dalvikvm-heap(1136): Forcing collection of SoftReferences for 6031876-byte allocation
04-24 12:31:38.553: D/dalvikvm(1136): GC_BEFORE_OOM freed 9K, 29% free 19829K/27816K, paused 730ms, total 732ms
04-24 12:31:38.553: E/dalvikvm-heap(1136): Out of memory on a 6031876-byte allocation.
04-24 08:56:04.143: E/AndroidRuntime(2853): FATAL EXCEPTION: main
04-24 08:56:04.143: E/AndroidRuntime(2853): Process: com.vardhan.android.navigationdrawerexample, PID: 2853
04-24 08:56:04.143: E/AndroidRuntime(2853): java.lang.OutOfMemoryError
04-24 08:56:04.143: E/AndroidRuntime(2853): at java.util.ArrayList.add(ArrayList.java:118)
04-24 08:56:04.143: E/AndroidRuntime(2853): at com.vardhan.android.navigationdrawerexample.BillingProductListView$1$13.onClick(BillingProductListView.java:349)
04-24 08:56:04.143: E/AndroidRuntime(2853): at android.view.View.performClick(View.java:4438)
04-24 08:56:04.143: E/AndroidRuntime(2853): at android.view.View$PerformClick.run(View.java:18422)
04-24 08:56:04.143: E/AndroidRuntime(2853): at android.os.Handler.handleCallback(Handler.java:733)
04-24 08:56:04.143: E/AndroidRuntime(2853): at android.os.Handler.dispatchMessage(Handler.java:95)
04-24 08:56:04.143: E/AndroidRuntime(2853): at android.os.Looper.loop(Looper.java:136)
04-24 08:56:04.143: E/AndroidRuntime(2853): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-24 08:56:04.143: E/AndroidRuntime(2853): at java.lang.reflect.Method.invokeNative(Native Method)
04-24 08:56:04.143: E/AndroidRuntime(2853): at java.lang.reflect.Method.invoke(Method.java:515)
04-24 08:56:04.143: E/AndroidRuntime(2853): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-24 08:56:04.143: E/AndroidRuntime(2853): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-24 08:56:04.143: E/AndroidRuntime(2853): at dalvik.system.NativeStart.main(Native Method)
【问题讨论】:
-
@Skynet 完成请检查
-
第349行是什么,请在此标出?错误总是出现在这一行吗?
-
@Skynet tempproduct_Code.add(tvCode.getText().toString());不知道怎么标记请帮忙
-
你如何定义
tempproduct_Code -
@Skynet private ArrayList
tempproduct_Code = new ArrayList ();
标签: android performance android-activity arraylist out-of-memory