Android String数组长度和循环

时间:2013-12-22 12:29:22

标签: android android-layout android-intent

我对String数组没什么问题。在一个课程中,我有简单的列表视图,有多种选择。最后,每个选项都放在String数组中并共享给下一个类。

SparseBooleanArray checked = listView.getCheckedItemPositions();
        ArrayList<String> selectedItems = new ArrayList<String>();
    for (int i = 0; i < checked.size(); i++) {
        // Item position in adapter
        int position = checked.keyAt(i);
        // Add sport if it is checked i.e.) == TRUE!
        if (checked.valueAt(i))
            selectedItems.add(adapter.getItem(position));
    }

    String[] outputStrArr = new String[selectedItems.size()];

    for (int i = 0; i < selectedItems.size(); i++) {
        outputStrArr[i] = selectedItems.get(i);
    }

   Intent intent = new Intent(getApplicationContext(),
            ReadComments.class);

    // Create a bundle object
    Bundle b = new Bundle();
    b.putStringArray("selectedItems", outputStrArr);

    // Add the bundle to the intent.
    intent.putExtras(b);

    // start the ResultActivity
    startActivity(intent);

在ReadComments课程中,我做了一个简单的方法:

public String[] tablica(){
        Bundle b = getIntent().getExtras();
        String[] resultArr = b.getStringArray("selectedItems");
        return resultArr;

    }

带回我的数据。当我把它放到同一个类中的另一个方法时:

        for(x=0;x<=resultArr.length;x++){
            if(resultArr[x] == nazwa){
                lat = mListaMar.get(TAG_SZER);
                longi = mListaMar.get(TAG_DLUG);
                // String laty = Float.toString(lat);
                name = mListaMar.get(TAG_MARKET);
                ustawMape();

            }
        }}

我有ArrayIndexOutOfBoundsException。 Pleasy帮助我这是一个简单的问题,但非常讨厌

12-22 13:23:50.055: E/AndroidRuntime(8735): FATAL EXCEPTION: main
12-22 13:23:50.055: E/AndroidRuntime(8735): java.lang.ArrayIndexOutOfBoundsException
12-22 13:23:50.055: E/AndroidRuntime(8735):     at com.flex.sklepik.ReadComments.updateList(ReadComments.java:235)
12-22 13:23:50.055: E/AndroidRuntime(8735):     at com.flex.sklepik.ReadComments$LoadComments.onPostExecute(ReadComments.java:318)
12-22 13:23:50.055: E/AndroidRuntime(8735):     at com.flex.sklepik.ReadComments$LoadComments.onPostExecute(ReadComments.java:1)
12-22 13:23:50.055: E/AndroidRuntime(8735):     at android.os.AsyncTask.finish(AsyncTask.java:417)
12-22 13:23:50.055: E/AndroidRuntime(8735):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
12-22 13:23:50.055: E/AndroidRuntime(8735):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
12-22 13:23:50.055: E/AndroidRuntime(8735):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 13:23:50.055: E/AndroidRuntime(8735):     at android.os.Looper.loop(Looper.java:130)
12-22 13:23:50.055: E/AndroidRuntime(8735):     at android.app.ActivityThread.main(ActivityThread.java:3701)
12-22 13:23:50.055: E/AndroidRuntime(8735):     at java.lang.reflect.Method.invokeNative(Native Method)
12-22 13:23:50.055: E/AndroidRuntime(8735):     at java.lang.reflect.Method.invoke(Method.java:507)
12-22 13:23:50.055: E/AndroidRuntime(8735):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
12-22 13:23:50.055: E/AndroidRuntime(8735):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
12-22 13:23:50.055: E/AndroidRuntime(8735):     at dalvik.system.NativeStart.main(Native Method)

P.S。 235线显示:

if(resultArr[x] == nazwa){

1 个答案:

答案 0 :(得分:6)

替换

 for(x=0;x<=resultArr.length;x++)

使用

 for(x=0;x<resultArr.length;x++)

为什么呢? 长度为3的数组的索引为0,1,2。在您的情况下,会出现问题,因为如果长度为3,您将尝试调用resultArr[3]