1

这是我非常非常精简的版本,仅包括布尔值及其周围的结构:

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 中,将按月打印给用户。

整个实际应用也提出了一个还债策略。我还没有对此进行测试,并将报告回来。任何反馈?让我知道!

4

3 回答 3

2

这是你的问题:

Caused by: java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
at com.---.---.DebtDataSource.payoffDebt(DebtDataSource.java:290)

您正在尝试访问超出数组范围的索引。查看ArrayIndexOutOfBoundsException文档。

你可以发布这个特定的行代码:

DebtDataSource.payoffDebt(DebtDataSource.java:290)

您需要检查变量bbb是否在数组的范围内。我认为这是问题所在,请尝试将您的代码更改为:

if ((indBal[p] <= 0) && (booIsPaid[bbb] == false) && (bbb <= booIsPaid.length)){
      booIsPaid[bbb++] = true;
} 

我还建议您bbb在循环代码的某个点将 的值重新设置为零。

希望能帮助到你!

于 2012-12-03T05:19:16.787 回答
2

下面的一些问题/意见:

  1. 不需要第一个循环,因为boolean []已经用值初始化为false

  2. 无需与boolean比较false,因为您可以简单地写if ((indBal[p] <= 0) && !booIsPaid[bbb]) {

  3. 在您的 while 循环中没有检查数组长度(导致您的问题)。添加如下:

    while (totalDebt > 0 && bbb <booIsPaid.length) {
      for (int i : rowCounter) { 
        if (indBal[p] <= 0 && !booIsPaid[bbb]  &&  bbb <booIsPaid.length) {
            booIsPaid[bbb++] = true;
        } 
      }
    }
    
于 2012-12-03T05:20:28.363 回答
1

首先,您不必将布尔数组中的每个值都设置为false. 默认情况下它们是这样设置的。

其次,您的循环确实看起来有点可疑。以下将解决您得到的 AIOOBException 。

while (totalDebt > 0) {
    for (int i : rowCounter) { 
        if (indBal[p] <= 0 && !booIsPaid[bbb]) {
            booIsPaid[bbb++] = true;
        } 
    }
    bbb = 0;
}
return monthTotal;

原因: bbbwhileand中迭代时无限制地增长for

于 2012-12-03T05:20:30.397 回答