I am using an ArrayList to loop through an application's contact list, and send SMS to these numbers for which I have the following used:
ArrayList<String> contacts = new ArrayList();
List<String> list = SM.getAllValue();
for(String string : list){
sb.append(string.toString());
contacts.add(string);
}
for(int i = 0; i <= contacts.size(); i++){
String numberToSend = contacts.get(i).toString();
sendSMS(numberToSend, sms);
}
The code SM.getAllValues()
returns all the user's contact preferences in List(s)
. The program works- the messages are sent but the application crashes every single time, giving the following in the logcat:
09-01 20:02:44.852: E/AndroidRuntime(19151): FATAL EXCEPTION: main
09-01 20:02:44.852: E/AndroidRuntime(19151): java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3
09-01 20:02:44.852: E/AndroidRuntime(19151): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
09-01 20:02:44.852: E/AndroidRuntime(19151): at java.util.ArrayList.get(ArrayList.java:311)
09-01 20:02:44.852: E/AndroidRuntime(19151): at com.vaw.selfhelp.SmsActivity$4.onClick(SmsActivity.java:163)
09-01 20:02:44.852: E/AndroidRuntime(19151): at android.view.View.performClick(View.java:2485)
09-01 20:02:44.852: E/AndroidRuntime(19151): at android.view.View$PerformClick.run(View.java:9080)
09-01 20:02:44.852: E/AndroidRuntime(19151): at android.os.Handler.handleCallback(Handler.java:587)
09-01 20:02:44.852: E/AndroidRuntime(19151): at android.os.Handler.dispatchMessage(Handler.java:92)
09-01 20:02:44.852: E/AndroidRuntime(19151): at android.os.Looper.loop(Looper.java:130)
09-01 20:02:44.852: E/AndroidRuntime(19151): at android.app.ActivityThread.main(ActivityThread.java:3687)
09-01 20:02:44.852: E/AndroidRuntime(19151): at java.lang.reflect.Method.invokeNative(Native Method)
09-01 20:02:44.852: E/AndroidRuntime(19151): at java.lang.reflect.Method.invoke(Method.java:507)
09-01 20:02:44.852: E/AndroidRuntime(19151): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
09-01 20:02:44.852: E/AndroidRuntime(19151): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
09-01 20:02:44.852: E/AndroidRuntime(19151): at dalvik.system.NativeStart.main(Native Method)
Line 163:
String numberToSend = contacts.get(i).toString();
Help please :)