在Unity中用返回值调用Android库中的方法

时间:2018-09-19 07:21:48

标签: android unity3d plugins

美好的一天!我已经为我的统一应用创建了一个简单的插件,我想在我的Android库中使用返回值调用该函数。这是我的代码段。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class TestAndroidPlugin : MonoBehaviour {
    // Use this for initialization
    public Text mText;
    void Start () {
        Debug.Log("Androidtion is to be called");

        AndroidJavaObject jo = new AndroidJavaObject("java.lang.String");
        var ajc = new AndroidJavaClass("com.teameut.mylibrary.TestingMyWekaJar");

        mText.text = jo.CallStatic<string>("getMessage");
        Debug.Log("Androidtion is called");
    }

    // Update is called once per frame
    void Update () {

    }
}

这是我要呼叫的图书馆。

package teameut.com.mylibrary;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import weka.classifiers.Classifier;

public class TestingMyWekaJar extends AppCompatActivity{

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Helper.DoSthInAndroid();

        Log.i("Unity", getMessage());
    }

    public String getMessage(){
        String excption = "";
        try {
            Classifier cls = (Classifier)weka.core.SerializationHelper.read(getAssets().open("iris.model"));
            excption = "quite loaded";
        } catch (Exception e) {
            e.printStackTrace();
            excption = e.toString();
        }
        return "Hello World " + excption;
    }
}

我很难用返回值调用该函数。请帮忙! 任何帮助将不胜感激。 :D

已编辑 添加了日志

09-19 18:21:29.080: E/mono(22068): Unhandled Exception: 

System.NullReferenceException: Object reference not set to an instance of an object
09-19 18:21:29.080: E/mono(22068):   at UnityEngine.AndroidJavaObject.Dispose (Boolean disposing) [0x00000] in <filename unknown>:0 
09-19 18:21:29.080: E/mono(22068):   at UnityEngine.AndroidJavaObject.Finalize () [0x00000] in <filename unknown>:0 
09-19 18:21:29.081: E/mono(22068): Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object
09-19 18:21:29.081: E/mono(22068):   at UnityEngine.AndroidJavaObject.Dispose (Boolean disposing) [0x00000] in <filename unknown>:0 
09-19 18:21:29.081: E/mono(22068):   at UnityEngine.AndroidJavaObject.Finalize () [0x00000] in <filename unknown>:0 
09-19 18:21:29.086: E/Unity(22068): AndroidJavaException: java.lang.ClassNotFoundException: com.teameut.mylibrary.TestingMyWekaJar
09-19 18:21:29.086: E/Unity(22068): java.lang.ClassNotFoundException: com.teameut.mylibrary.TestingMyWekaJar
09-19 18:21:29.086: E/Unity(22068):     at java.lang.Class.classForName(Native Method)
09-19 18:21:29.086: E/Unity(22068):     at java.lang.Class.forName(Class.java:400)
09-19 18:21:29.086: E/Unity(22068):     at java.lang.Class.forName(Class.java:326)
09-19 18:21:29.086: E/Unity(22068):     at com.unity3d.player.UnityPlayer.nativeRender(Native Method)
09-19 18:21:29.086: E/Unity(22068):     at com.unity3d.player.UnityPlayer.c(Unknown Source)
09-19 18:21:29.086: E/Unity(22068):     at com.unity3d.player.UnityPlayer$c$1.handleMessage(Unknown Source)
09-19 18:21:29.086: E/Unity(22068):     at android.os.Handler.dispatchMessage(Handler.java:106)
09-19 18:21:29.086: E/Unity(22068):     at android.os.Looper.loop(Looper.java:232)
09-19 18:21:29.086: E/Unity(22068):     at com.unity3d.player.UnityPlayer$c.run(Unknown Source)
09-19 18:21:29.086: E/Unity(22068): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.teameut.mylibrary.TestingMyWekaJar" on path: DexPathList[[zip file "/data/app/com.teameut.TestSampleTest-1/base.apk"],nativeLibraryDirectories=[/data/app/com.teameut.TestSampleTest-1/lib/arm, /data/app/com.teameut.TestSampleTest-1/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib]]
09-19 18:21:29.086: E/Unity(22068):     at dalvik.system.BaseD

0 个答案:

没有答案