-1

如何解决此错误 The operator += is undefined for the argument type(s) String[][], String

  SmsMessage[] msgs = null;
   String [][]str = null;  

    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";     
4

1 回答 1

2

该错误几乎说明了一切 - 您不能+=在 Java 中使用数组。为什么不使用 List 来构建 String 值,然后在完成后将其转换为数组?

    List<String> str = new ArrayList<String>();  

    for (int i=0; i<msgs.length; i++){
        msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);                
        str.add("SMS from " + msgs[i].getOriginatingAddress());                     
        str.add(" :");
        ...

    String[] strArray = str.toArray(new String[str.size()]);
于 2012-08-31T12:45:02.047 回答