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

标签 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 签名的地方崩溃了

最佳答案

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

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

更改此内容

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

关于java.lang.ClassCastException : [Ljava. lang.String;无法转换为 java.lang.String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36470627/

相关文章:

java - 如何在 Hibernate 中序列化集合属性?

Java - 从递归函数返回

java - 为什么我的 .isSelected() 方法不起作用?

java - 修复 Gridlayout 转换为 ImageView 时出现的错误

java - 使用 Spring JdbcTemplate 中的 call 调用存储函数

java - 正确对齐控制台输出表中的列并从零开始计数

java - ClassCastException 与 "inconvertible types"编译错误

java - Java 中的 ClassCastException

java - Retrofit2: ClassCastException: java.util.ArrayList 无法转换为 retrofit2.Call

spring-mvc - Spring MVC——java.lang.ClassCastException