小编Edw*_*uez的帖子

带有导入库的Android Studio java.lang.NoSuchMethodError

我按照以下步骤导入了commons-codec-1.10.jar:

  1. 在de app目录下创建了一个libs目录
  2. 手动复制libs目录中的.jar
  3. 右键单击android-studio中的.jar,然后单击Add as library

在我的build.grade中添加了这一行

compile fileTree(dir: 'libs', include: ['*.jar'])
Run Code Online (Sandbox Code Playgroud)

在我的课上我导入了这样的库:

import org.apache.commons.codec.binary.Base64;
Run Code Online (Sandbox Code Playgroud)

然后我尝试在Base64中访问encodeBase64String静态方法,如下所示:

public static class DoThisThing {
    public String DoThisOtherThing() {
        String hashed = "hello";
        String hash = Base64.encodeBase64String(hashed.getBytes());
        return hash;
    }
}

public class ActivityThing extends AppCompatActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_thing);
        String hash = DoThisThing.DoThisOtherThing();
        System.out.println(hash);
    }
}
Run Code Online (Sandbox Code Playgroud)

没有错,即使我编译,除非我运行应用程序,它会抛出以下错误,应用程序关闭:

11-03 09:41:27.719 2390-2476/com.myproject E/AndroidRuntime:  Caused by: java.lang.NoSuchMethodError: No static method encodeBase64String([B)Ljava/lang/String; in class Lorg/apache/commons/codec/binary/Base64; or its super classes (declaration of 'org.apache.commons.codec.binary.Base64' …
Run Code Online (Sandbox Code Playgroud)

java android static-methods android-studio android-runtime

6
推荐指数
2
解决办法
7696
查看次数