Android - 该方法未定义

时间:2015-02-24 19:16:05

标签: android arrays charsequence

尝试转换为CharSequence []时,我一直收到此错误。

方法toArray(CharSequence [])未定义类型String

这是我的代码

              CharSequence[] cs = abbrev.toArray(new CharSequence[abbrev.length()]);

缩写只是将句子分成第一个字符 (Hello World - > HW)

       String[] result = matches.toString().split("\\s+");
        // The string we'll create

        String abbrev = "";

           // Loop over the results from the string splitting
           for (int i = 0; i < result.length; i++){

               // Grab the first character of this entry
               char c = result[i].charAt(0);

               // If its a number, add the whole number
               if (c >= '0' && c <= '9'){
                   abbrev += result[i];
               }

               // If its not a number, just append the character
               else{
                   abbrev += c;
               }
           }

任何想法?

由于

2 个答案:

答案 0 :(得分:0)

由于abbrev是一个字符串,因此您无法在其上调用toArray。首先将其转换为CharSequence。 例如,

CharSequence[] abbrevCs = abbrev; // it's converted automatically
CharSequence[] cs = abbrevCs.toArray(new CharSequence[abbrev.length()]);

答案 1 :(得分:0)

您不必将String转换为CharSequence,因为String CharSequence。您只需将String对象分配给CharSequence对象,转换就是隐式的。有关详情,请参阅此处:How to convert a String to CharSequence?