这是我非常非常精简的版本,仅包括布尔值及其周围的结构:
public int payoffDebt(Double totalDebt) {
boolean booIsPaid[] = new boolean[c.getCount()];
int bb = 0;
int bbb = 0;
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
booIsPaid[bb++] = false;
}
while (totalDebt > 0) {
for (int i : rowCounter) {
if ((indBal[p] <= 0) && (booIsPaid[bbb] == false)) {
booIsPaid[bbb++] = true;
}
}
}
return monthTotal;
}
这就是我正在尝试做的事情,在早期我循环用户“债务”并为每个名为 booIsPaid 的人分配一个布尔值。它们设置为 false,这很好用。但是,在下面,我认为我没有正确地遍历它们,因为在我的 Android 应用程序中,我在调用它的 Activity 中得到了 Window Leaking LogCat's。我注释掉我设置的位置true
并且一切运行良好。有人能看出哪里不对吗?
编辑:堆栈跟踪:
12-02 21:14:35.121: E/AndroidRuntime(8832): FATAL EXCEPTION: AsyncTask #1
12-02 21:14:35.121: E/AndroidRuntime(8832): java.lang.RuntimeException: An error occured while executing doInBackground()
12-02 21:14:35.121: E/AndroidRuntime(8832): at android.os.AsyncTask$3.done(AsyncTask.java:299)
12-02 21:14:35.121: E/AndroidRuntime(8832): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-02 21:14:35.121: E/AndroidRuntime(8832): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-02 21:14:35.121: E/AndroidRuntime(8832): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-02 21:14:35.121: E/AndroidRuntime(8832): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-02 21:14:35.121: E/AndroidRuntime(8832): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-02 21:14:35.121: E/AndroidRuntime(8832): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-02 21:14:35.121: E/AndroidRuntime(8832): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-02 21:14:35.121: E/AndroidRuntime(8832): at java.lang.Thread.run(Thread.java:856)
12-02 21:14:35.121: E/AndroidRuntime(8832): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
12-02 21:14:35.121: E/AndroidRuntime(8832): at com.---.---.DebtDataSource.payoffDebt(DebtDataSource.java:290)
12-02 21:14:35.121: E/AndroidRuntime(8832): at com.---.---.Planner$PlannerTask.doInBackground(Planner.java:73)
12-02 21:14:35.121: E/AndroidRuntime(8832): at com.---.---.Planner$PlannerTask.doInBackground(Planner.java:1)
12-02 21:14:35.121: E/AndroidRuntime(8832): at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-02 21:14:35.121: E/AndroidRuntime(8832): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-02 21:14:35.121: E/AndroidRuntime(8832): ... 5 more
12-02 21:14:35.128: W/ActivityManager(291): Force finishing activity com.---.---/.Planner
12-02 21:14:35.175: W/ActivityManager(291): Force finishing activity com.---.---/.ManageDebts
12-02 21:14:35.285: W/ActivityManager(291): Duplicate finish request for ActivityRecord{41e0b390 com.---.---/.Planner}
12-02 21:14:35.496: I/ActivityManager(291): Displayed com.---.---/.Planner: +557ms
12-02 21:14:35.605: E/WindowManager(8832): Activity com.---.---.Planner has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4175e930 that was originally added here
12-02 21:14:35.605: E/WindowManager(8832): android.view.WindowLeaked: Activity com.---.---.Planner has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@4175e930 that was originally added here
12-02 21:14:35.605: E/WindowManager(8832): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:374)
12-02 21:14:35.605: E/WindowManager(8832): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:292)
12-02 21:14:35.605: E/WindowManager(8832): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
12-02 21:14:35.605: E/WindowManager(8832): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
12-02 21:14:35.605: E/WindowManager(8832): at android.view.Window$LocalWindowManager.addView(Window.java:547)
12-02 21:14:35.605: E/WindowManager(8832): at android.app.Dialog.show(Dialog.java:277)
12-02 21:14:35.605: E/WindowManager(8832): at com.---.---.Planner$PlannerTask.onPreExecute(Planner.java:56)
12-02 21:14:35.605: E/WindowManager(8832): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
12-02 21:14:35.605: E/WindowManager(8832): at android.os.AsyncTask.execute(AsyncTask.java:534)
12-02 21:14:35.605: E/WindowManager(8832): at com.---.---.Planner.onCreate(Planner.java:36)
12-02 21:14:35.605: E/WindowManager(8832): at android.app.Activity.performCreate(Activity.java:5008)
12-02 21:14:35.605: E/WindowManager(8832): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-02 21:14:35.605: E/WindowManager(8832): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-02 21:14:35.605: E/WindowManager(8832): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-02 21:14:35.605: E/WindowManager(8832): at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-02 21:14:35.605: E/WindowManager(8832): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-02 21:14:35.605: E/WindowManager(8832): at android.os.Handler.dispatchMessage(Handler.java:99)
12-02 21:14:35.605: E/WindowManager(8832): at android.os.Looper.loop(Looper.java:137)
12-02 21:14:35.605: E/WindowManager(8832): at android.app.ActivityThread.main(ActivityThread.java:4745)
12-02 21:14:35.605: E/WindowManager(8832): at java.lang.reflect.Method.invokeNative(Native Method)
12-02 21:14:35.605: E/WindowManager(8832): at java.lang.reflect.Method.invoke(Method.java:511)
12-02 21:14:35.605: E/WindowManager(8832): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-02 21:14:35.605: E/WindowManager(8832): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-02 21:14:35.605: E/WindowManager(8832): at dalvik.system.NativeStart.main(Native Method)
编辑:我已经修改了所有内容,这是我的新方法:
while (totalDebt > 0) {
booIsPaid = false;
remainingBalance = 0;
// Increment month
monthTotal = monthTotal + 1;
// update INDIVIDUAL debt Here
for (int i : rowCounter) {
nameList.add(indName[r++]);
// Zero Balance, set Boolean true, add to Remainder
if ((indBal[p] <= 0) && !booIsPaid) {
booIsPaid = true;
remainingbalance = remainingbalance + indPay[q];
indPay[q] = 0;
indBal[p] = 0;
payList.add(Double.valueOf(IndPay));
// First balance after zero; set Boolean false, add to remainder to payment.
} else if ((indBal[p] > 0) && booIsPaid) {
booIsPaid = false;
indPay[q] = indPay[q] + remainingbalance;
indPay[q] = Math.round(indPay[q] * 100.00) / 100.00;
IndPay = myFormat.format(indPay[q]);
payList.add(Double.valueOf(IndPay));
// Other remaining balances
} else if ((indBal[p] > 0) && !booIsPaid) { indPay[q] = 0;
indPay[q] = Math.round(indPay[q] * 100.00) / 100.00;
IndPay = myFormat.format(indPay[q]);
payList.add(Double.valueOf(IndPay));
}
// indPay[q] (should or should not include remainder here!
indBal[p] = indBal[p] - (indPay[q] - interestFee);
totalDebt = totalDebt - (indPay[q++] - interestFee);
IndBal = myFormat.format(indBal[p++]);
indBalList.add(Double.valueOf(IndBal));
feeList.add(Double.valueOf(InterestFee));
}
}
注意:输出存储在此处的四个 ArrayList 中,将按月打印给用户。
整个实际应用也提出了一个还债策略。我还没有对此进行测试,并将报告回来。任何反馈?让我知道!