繁体   English   中英

尝试调用类构造函数时在Unity中获取AndroidJavaException

[英]Getting AndroidJavaException in Unity when trying to call class constructor

我在Unity中使用此C#代码访问Java类是.aar lib:

 AndroidJavaClass ajc;
    private AndroidJavaObject ajo;
    // Use this for initialization
    void Start () {
        ajc = new AndroidJavaClass("com.example.pc.superpoweredsdk.SuperPoweredPlayerWrapper");
        ajo = ajc.Get<AndroidJavaObject>("currentActivity");
    }

但我在android上的logcat中收到此错误:

AndroidJavaException:java.lang.NoSuchFieldError:否“ Ljava / lang / Object;” 类别“ Lcom / example / pc / superpoweredsdk / SuperPoweredPlayerWrapper”中的“ currentActivity”字段; 或其超类07-01 12:31:08.640 1467 1485 I Unity:java.lang.NoSuchFieldError:否“ Ljava / lang / Object;” 类别“ Lcom / example / pc / superpoweredsdk / SuperPoweredPlayerWrapper”中的“ currentActivity”字段; 或其超类

这是我要调用的Java类和函数:

public class SuperPoweredPlayerWrapper {
    public SuperPoweredPlayerWrapper(Context context) {
        int sampleRate = 44100;
        int bufferSize = 512;
        AssetFileDescriptor fd = context.getResources().openRawResourceFd(R.raw.lycka);
        int fileOffset = (int)fd.getStartOffset();
        int fileLength = (int)fd.getLength();
        try {
            fd.getParcelFileDescriptor().close();
        } catch (IOException e) {
            android.util.Log.d("", "Close error.");
        }
        SuperpoweredPlayer(sampleRate, bufferSize, context.getPackageResourcePath(), fileOffset, fileLength);
    }

    private native void SuperpoweredPlayer(int sampleRate, int bufferSize, String apkPath, int fileOffset, int fileLength);
    public native void playPause(boolean play);
    public native void setTempo(double value);

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

如何从Unity调用带有上下文参数的此类构造函数?

currentActivitycom.unity3d.player.UnityPlayer的成员

因此,此代码将获取上下文

AndroidJavaClass ajc;
AndroidJavaObject ajo,context;
ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        ajo = ajc.Get<AndroidJavaObject>("currentActivity");
context = ajo.Call<AndroidJavaObject>("getApplicationContext");

然后您可以根据上下文执行任何操作。

调用构造函数:

AndroidJavaObject yourClassObject = new AndroidJavaObject("com.example.pc.superpoweredsdk.SuperPoweredPlayerWrapper",new object[]{context});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM