如何在switch-case上实现outofbound

时间:2014-03-25 14:19:54

标签: java android arrays string web-services

我有下一个错误:java.lang.IndexOutOfBoundsException:Index:0,Size:0我希望有人可以帮我解决这个问题。我想我需要检查String是否为null,但我不知道如何在下一个代码中实现它。

这是我的代码:

03-25 15:22:40.893: E/AndroidRuntime(617): FATAL EXCEPTION: main
03-25 15:22:40.893: E/AndroidRuntime(617): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
03-25 15:22:40.893: E/AndroidRuntime(617):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
03-25 15:22:40.893: E/AndroidRuntime(617):  at java.util.ArrayList.get(ArrayList.java:304)
03-25 15:22:40.893: E/AndroidRuntime(617):  at android.widget.ArrayAdapter.getItem(ArrayAdapter.java:337)
03-25 15:22:40.893: E/AndroidRuntime(617):  at android.widget.AdapterView.getItemAtPosition(AdapterView.java:763)
03-25 15:22:40.893: E/AndroidRuntime(617):  at it.nad.cartellecliniche.PatientPageActivity$1.onItemClick(PatientPageActivity.java:224)
03-25 15:22:40.893: E/AndroidRuntime(617):  at android.widget.AdapterView.performItemClick(AdapterView.java:298)
03-25 15:22:40.893: E/AndroidRuntime(617):  at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
03-25 15:22:40.893: E/AndroidRuntime(617):  at it.nad.cartellecliniche.PatientPageActivity.fillVisiteList(PatientPageActivity.java:253)
03-25 15:22:40.893: E/AndroidRuntime(617):  at it.nad.cartellecliniche.PatientPageActivity.onTaskComplete(PatientPageActivity.java:593)
03-25 15:22:40.893: E/AndroidRuntime(617):  at it.nad.cartellecliniche.asynctask.SearchVisiteTask.onPostExecute(SearchVisiteTask.java:263)
03-25 15:22:40.893: E/AndroidRuntime(617):  at it.nad.cartellecliniche.asynctask.SearchVisiteTask.onPostExecute(SearchVisiteTask.java:1)
03-25 15:22:40.893: E/AndroidRuntime(617):  at android.os.AsyncTask.finish(AsyncTask.java:631)
03-25 15:22:40.893: E/AndroidRuntime(617):  at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-25 15:22:40.893: E/AndroidRuntime(617):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
03-25 15:22:40.893: E/AndroidRuntime(617):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 15:22:40.893: E/AndroidRuntime(617):  at android.os.Looper.loop(Looper.java:137)
03-25 15:22:40.893: E/AndroidRuntime(617):  at android.app.ActivityThread.main(ActivityThread.java:4745)
03-25 15:22:40.893: E/AndroidRuntime(617):  at java.lang.reflect.Method.invokeNative(Native Method)
03-25 15:22:40.893: E/AndroidRuntime(617):  at java.lang.reflect.Method.invoke(Method.java:511)
03-25 15:22:40.893: E/AndroidRuntime(617):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-25 15:22:40.893: E/AndroidRuntime(617):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-25 15:22:40.893: E/AndroidRuntime(617):  at dalvik.system.NativeStart.main(Native Method)



    public void onTaskComplete(Object[] output) {
        super.onTaskComplete(output);
        Integer resultOperation = (Integer) output[0];
         if (output != null && resultOperation != 500) {
            String typeTask = (String) output[1];
                if (typeTask.equals("SearchListVisite")) {
                    switch (resultOperation) {

                        case 200: {
                        Visita[] visite_current_patient = (Visita[]) output[2];                         
                        fillVisiteList(visite_current_patient);
                        break;
                    }   
                        case 204: {
                        Visita[] visitaEmpty = createVisteListEmpty();
                        fillVisiteList(visitaEmpty);
                        break;
                    }
                    }
            }
        } else {
            CharSequence testo = "Errore!";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(this, testo, duration);
            toast.setGravity(Gravity.CENTER, 0, 0);
            toast.show();
            onBackPressed();
        }

    }

2 个答案:

答案 0 :(得分:0)

移动线:

Integer resultOperation = (Integer) output[0];
在if语句中使用

来阻止NullPointerException。并添加条件:

output.length >= 3

我做了一次重构,并希望我没有破坏任何东西。但是你没有提供所有类型来使它在我的IDE中编译:

public void onTaskComplete(Object[] output) {
    super.onTaskComplete(output);
    if (output != null && output.length >= 3) {
        Integer resultOperation = (Integer) output[0];
        String typeTask = (String) output[1];
        if (typeTask.equals("SearchListVisite")) {
            switch (resultOperation) {
            case 200:
                Visita[] visite_current_patient = (Visita[]) output[2];                         
                fillVisiteList(visite_current_patient);
                break;
            case 204:
                Visita[] visitaEmpty = createVisteListEmpty();
                fillVisiteList(visitaEmpty);
                break;
            }
        }
    } else {
        CharSequence testo = "Errore!";
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(this, testo, duration);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
        onBackPressed();
    }
}

答案 1 :(得分:0)

我发现这个解决方案有一个不同的If语句:

if(visite_current patient.length< 2)

  public void onTaskComplete(Object[] output) {
    super.onTaskComplete(output);   
    Integer resultOperation = (Integer) output[0];

    if (output != null && resultOperation != 500) {
        String typeTask = (String) output[1];
        if (typeTask.equals("SearchListVisite")) {
            switch (resultOperation) {
            case 200:
                Visita[] visite_current_patient = (Visita[]) output[2];
                if(visite_current_patient.length < 2) {
                    visite_current_patient = createVisteListEmpty();
                }
                fillVisiteList(visite_current_patient);
                break;
            case 204:
                Visita[] visitaEmpty = createVisteListEmpty();
                fillVisiteList(visitaEmpty);
                break;
            }

    } else {
        CharSequence testo = "Errore!";
        int duration = Toast.LENGTH_SHORT;
        Toast toast = Toast.makeText(this, testo, duration);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
        onBackPressed();
    }
    } 
}