-1

this is my application code below which nto going to intent part i used debug i find is not going after line xmlResponse2[0][i]=test[i]; wht i mistake?? can anyone help me is not launch any activity and code broke after xmlResponse2[0][i]=test[i]; this line

  public void onReceive(Context context, Intent intent) 
{
    //---get the SMS message passed in---
    Bundle bundle = intent.getExtras();        
    SmsMessage[] msgs = null;
    String str = "";  

    if (bundle != null)
    {
        //---retrieve the SMS message received---
        Object[] pdus = (Object[]) bundle.get("pdus");
        msgs = new SmsMessage[pdus.length];            
        for (int i=0; i<msgs.length; i++){
            msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
            str += "SMS from " + msgs[i].getOriginatingAddress();                     
            str += " :";
            str += msgs[i].getMessageBody().toString();
            str += "\n";  



        String[][] xmlResponse2= null;
        String[] test = str.split("\n");
        xmlResponse2= new String[0][test.length];
        for (int i = 0; i < test.length; i++) {
            xmlResponse2[0][i]=test[i];
             Intent l = new Intent(context,AgAppMenu.class);
             l.putExtra("msg",xmlResponse2);
             l.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
             context.startActivity(l);
        }

        //---display the new SMS message---
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
Hayya ANAM
  • 575
  • 2
  • 14
  • 38

2 Answers2

0

This statement:

xmlResponse2= new String[0][test.length];

creates an array with 0 elements (ie: with NO elements). Maybe you want to create an array with 1 element, like this:

xmlResponse2= new String[1][test.length];
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • how i start new activity with using this parameter l.putExtra("msg",xmlResponse2); – Hayya ANAM Sep 03 '12 at 13:13
  • is this really true? doesn't new String[0][test.length]; actually create these second elements in the first element indexed 0? – Carnal Sep 03 '12 at 13:16
  • @Carnal i too hope so the index starts from 0 in an array (its a 2 dimensional array that he mean to create after all) – Athul Harikumar Sep 03 '12 at 13:17
  • @Carnal i 2 agree 2 that the index starts from 0 itself i dont get the point why he want it from 1 onwards – Athul Harikumar Sep 03 '12 at 13:22
  • my Application Recievd sms in this format 00 in line1 LOG in line2 00 in line 3 Adeel Aslam in line 4 – Hayya ANAM Sep 03 '12 at 13:22
  • how i pass this sms format as a parameter to another activity and print sms is 00 in line 1 LOg in line 2 00 in line 3 Adeel Aslam in line 4 – Hayya ANAM Sep 03 '12 at 13:26
  • @Carnal `new String[0][test.length]` creates a 2-dimensional array that has no elements (because the size of the first dimenstion is zero). It does **not** create an array of `String[test.length]` and assign that to the first element of xmlResponse2! If that is what you want to do you need to do this: `xmlResponse2[0] = new String[test.length]` – David Wasser Sep 03 '12 at 13:31
  • no i want read an sms which is this format 00 in line 1 LOG in line 2 00 in line 3 Adeel Aslam in line 4 – Hayya ANAM Sep 03 '12 at 13:39
  • @David Wasser this is not true mate, I just tested and it works fine with new String[0][size]; – Carnal Sep 03 '12 at 13:47
  • Since we are creating 2d array, the last "[]" will decide the size for each array in 2d. – Carnal Sep 03 '12 at 13:52
  • @Carnal Can't be. Try this: `String[][] foo = new String[0][10]; System.out.println("Size "+foo.length);` You will see that `foo.length` is zero. When you create the array you tell it that the first dimension has zero elements. – David Wasser Sep 03 '12 at 15:46
  • This line `xmlResponse2[0][i]=test[i];` will generate an ArrayIndexOutOfBoundsException with the code as it is. – David Wasser Sep 03 '12 at 15:48
  • @DavidWasser friend an arrays index starts from 0, String[][] foo = new String[0][10]; System.out.println("Size "+foo.length); you are getting 0 as you cannot find the length like that tryfoo[0].length as its two dimenssional – Athul Harikumar Sep 04 '12 at 05:04
  • @droidhot You need to read about how multidimensional arrays work. `foo` in this case is an array of String arrays. Of course you can get its length. In this example`foo[0].length` will also gives you an ArrayIndexOutOfBoundsException because as I said in my answer, the variable is an array that contains zero entries. Please buy a Java book and read it before making comments like this. – David Wasser Sep 04 '12 at 09:16
  • @DavidWasser yap it has zero entries as you havent assigned any values but is a[0]="kbhdkha"; valid? dear friend an arrays index start from 0 only ie if an array of length 5 is there then the index is from 0-4 just refer some docs – Athul Harikumar Sep 04 '12 at 09:20
  • @DavidWasser refer this http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html – Athul Harikumar Sep 04 '12 at 09:21
0
 xmlResponse2= new String[test.length];
    for (int i = 0; i < test.length; i++) {
        xmlResponse2[i]=test[i];

    }
       Intent l = new Intent(context,AgAppMenu.class);
         l.putExtra("msg",xmlResponse2);
        // l.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);   
         context.startActivity(l);
Athul Harikumar
  • 2,501
  • 18
  • 16