-2

I'm new to android and having a problem with my first application that I have extended by using the Hello World. Logcat is appended. Can anyone please help why the application shuts down ?

09-27 19:05:57.442: E/AndroidRuntime(528): FATAL EXCEPTION: main
09-27 19:05:57.442: E/AndroidRuntime(528): java.lang.RuntimeException: Unable to start receiver com.livelihood.hello.world.ScanSMSReceiver: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
09-27 19:05:57.442: E/AndroidRuntime(528):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2126)
09-27 19:05:57.442: E/AndroidRuntime(528):  at android.app.ActivityThread.access$1500(ActivityThread.java:123)
09-27 19:05:57.442: E/AndroidRuntime(528):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1197)
09-27 19:05:57.442: E/AndroidRuntime(528):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-27 19:05:57.442: E/AndroidRuntime(528):  at android.os.Looper.loop(Looper.java:137)
09-27 19:05:57.442: E/AndroidRuntime(528):  at android.app.ActivityThread.main(ActivityThread.java:4424)
09-27 19:05:57.442: E/AndroidRuntime(528):  at java.lang.reflect.Method.invokeNative(Native Method)
09-27 19:05:57.442: E/AndroidRuntime(528):  at java.lang.reflect.Method.invoke(Method.java:511)
09-27 19:05:57.442: E/AndroidRuntime(528):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-27 19:05:57.442: E/AndroidRuntime(528):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-27 19:05:57.442: E/AndroidRuntime(528):  at dalvik.system.NativeStart.main(Native Method)
09-27 19:05:57.442: E/AndroidRuntime(528): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
09-27 19:05:57.442: E/AndroidRuntime(528):  at com.livelihood.hello.world.ScanSMSReceiver.onReceive(ScanSMSReceiver.java:40)
09-27 19:05:57.442: E/AndroidRuntime(528):  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2119)
09-27 19:05:57.442: E/AndroidRuntime(528):  ... 10 more
PeeHaa
  • 71,436
  • 58
  • 190
  • 262

1 Answers1

0

In line #40 in your ScanSMSReceiver.java class you're using an Array object that you did not instantiate properly.

Then error is telling you that you're trying to asccess index 0 from an array that has 0 objects in it. If you try and access an index which is more than 1 less of the number of items (number of items in an array -1) then your program will throw a

java.lang.ArrayIndexOutOfBoundsException

and crash your program.

Go through your code and check to see where you declared your Array object and instantiate it with a specific size

int[] myArray = new int[5];

(Or whichever type of array you're using) Then when you try and access index 0 it will no longer crash.

zabawaba99
  • 507
  • 1
  • 6
  • 13
  • though it all sounded greek to me but I'm really glad for your help sir :)...I'll do as you have guided and get back to you if you can still help. Can you please look through the line 39 and 40 of my ScanSMSReceived.java class which I attach here Account[] accounts = AccountManager.get(context).getAccountsByType("com.google"); final String primaryEmail = accounts[0].name; – user1703625 Sep 27 '12 at 14:42
  • It looks like AcountManager.get(context).getAccountsByType("com.google"); is returning an empty array so when you try and do accounts[0].name; it crashes because there is no item in it. I would need you to post the method or class that you are calling this from to help ya out more. Edit your question above and copy and paste you code – zabawaba99 Sep 27 '12 at 15:20
  • public class ScanSMSReceiver extends BroadcastReceiver { Override public void onReceive(Context context, Intent intent) { Account[] accounts = AccountManager.get(context).getAccountsByType("com.google"); final String primaryEmail = accounts[0].name; – user1703625 Sep 27 '12 at 18:57
  • please help sorry for the delayed respone – user1703625 Sep 27 '12 at 19:48
  • Check out the answer on this. Its exactly what you want http://stackoverflow.com/questions/2112965/how-to-get-the-android-devices-primary-e-mail-address – zabawaba99 Sep 27 '12 at 22:04