我使用.so库并收到NoSuchMethodError错误。

huangapple go评论76阅读模式
英文:

I use .so library and get NoSuchMethodError error

问题

我使用.so库,如下所示:

package ru.integrics.mobileschool.view.activity;

import android.util.Base64;

public class MainActivity {

    public MainActivity(){
        System.loadLibrary("x2");
    }
    public native String x01(String str);
    public String get(String str){
        String key = Base64.encodeToString(x01(str.substring(0, str.length() / 2)).getBytes(), Base64.NO_WRAP);
        return key;
    }

}

但是我得到了错误:

JNI DETECTED ERROR IN APPLICATION: 
JNI CallObjectMethodV called with pending exception java.lang.NoSuchMethodError: 
no non-static method "Lru/integrics/mobileschool/view/activity/MainActivity;.getPackageManager()Landroid/content/pm/PackageManager;"

我尝试将方法设置为静态,但不起作用。

英文:

I use .so library, as this

package ru.integrics.mobileschool.view.activity;

import android.util.Base64;

public class MainActivity {

    public MainActivity(){
        System.loadLibrary("x2");
    }
    public native String x01(String str);
    public String get(String str){
        String key = Base64.encodeToString(x01(str.substring(0, str.length() / 2)).getBytes(), Base64.NO_WRAP);
        return key;
    }

}

But i get error:

JNI DETECTED ERROR IN APPLICATION: 
JNI CallObjectMethodV called with pending exception java.lang.NoSuchMethodError: 
no non-static method "Lru/integrics/mobileschool/view/activity/MainActivity;.getPackageManager()Landroid/content/pm/PackageManager;"

I tried set methods static, but not working

答案1

得分: 0

您将System.loadLibrary("x2"); 放错了地方。您应该将它放在一个静态代码块中:

package ru.integrics.mobileschool.view.activity;

import android.util.Base64;

public class MainActivity {

    static {
        System.loadLibrary("x2");
    }

    public MainActivity(){
        ...
    }
    public native String x01(String str);
    public String get(String str){
        String key = Base64.encodeToString(x01(str.substring(0, str.length() / 2)).getBytes(), Base64.NO_WRAP);
        return key;
    }

}

之后,您需要确认您已正确添加了libx2.so 并且存在一个x01(String) 函数。

英文:

You are placing System.loadLibrary("x2"); wrongly. You should place it in a static block instead:

package ru.integrics.mobileschool.view.activity;

import android.util.Base64;

public class MainActivity {

    static {
        System.loadLibrary("x2");
    }

    public MainActivity(){
        ...
    }
    public native String x01(String str);
    public String get(String str){
        String key = Base64.encodeToString(x01(str.substring(0, str.length() / 2)).getBytes(), Base64.NO_WRAP);
        return key;
    }

}

And after that you have to confirm you have properly added libx2.so and there is a x01(String) function inside.

答案2

得分: 0

如果您阅读错误信息,您会发现它在抱怨一个缺少的Java方法,因为您的MainActivity类没有继承android.app.Activity类,该类提供了所需的getPackageManager方法。

英文:

If you read the error you see it is complaining about a missing Java method because your MainActivity class does not inherit from the android.app.Activity class, which provides the required getPackageManager method.

huangapple
  • 本文由 发表于 2023年2月9日 02:54:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75390474.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定