java.lang.ClassCastException:[Ljava.lang.String;无法强制转换为java.lang.String

时间:2016-04-07 08:25:34

标签: java classcastexception

我有这个错误。这是我的代码:

GmailSettingsService service = new GmailSettingsService(APPLICATION_NAME, DOMAIN_NAME, null, null){
        @Override
        public void setUserCredentials(String username, String password)
                throws AuthenticationException {
            // Nothing to do here, just Overriding the old method and setting it to null so we can later setOauthCredentials to the service
        }};

    service.setOAuth2Credentials(credential);
    List users = new ArrayList();
    for (int i = 0; i < emailsData.size(); i++)
    {
        users.add(emailsData.get(0).get(i).split("@"));

        String signature = "some html code";
        String escaped = StringEscapeUtils.escapeHtml4(signature).toString();

        service.changeSignature(users, escaped);

        users.remove(0);
    }

IDE将我发送到service.changeSignature(users, escaped);,但下一个例外:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.String
at gmailsettings.GmailSettingsService.changeSignature(GmailSettingsService.java:723)
at Controller.updateSignature(Controller.java:306)
at Controller.main(Controller.java:126)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

任何解决方案?该程序崩溃了它应该将html代码转换为编码字符串并使用该编码字符串更新Gmail签名

1 个答案:

答案 0 :(得分:5)

您正在向users添加字符串数组。

users.add(emailsData.get(0).get(i).split("@"));

使用

更改此设置
users.add(emailsData.get(0).get(i).split("@")[0]);