我试图让C#对java回调起作用。我按照"界面"本指南中的部分:https://mono.github.io/Embeddinator-4000/android-callbacks.html
问题是我在Android Studio中遇到错误:
致命的致命 EXCEPTION:System.NotSupportedException:无法找到Invoker 键入' MyProject.Interfaces.ICallback'。被链接了吗?
我正在使用Android Studio 3.0
为什么找不到我的调用者?我多次查看指南,但我找不到我忘记的或我做错了什么
我的.Net代码:
namespace MyProject.Classes
{
[Register("mono.embeddinator.android.CallbackTester")]
public class CallbackTester : Java.Lang.Object
{
//callback test method
[Export("process")]
public static void Process(ICallback callback)
{
callback.OnSuccess("this is a string defined in C#");
}
//callback test method
[Export("processno2")]
public void Processno2(ICallback callback)
{
callback.OnSuccess("this is a string defined in C#");
}
}
}
.NET界面:
namespace MyProject.Interfaces
{
[Register("mono.embeddinator.android.ICallback")]
public interface ICallback : IJavaObject
{
[Export("onSuccess")]
void OnSuccess(string test);
}
}
Java接口:( = Visual Studio中定义的.java文件)
package mono.embeddinator.android;
public interface ICallback
{
void onSuccess(String test);
}
调用器:
namespace MyProject.Invokers
{
class ICallbackInvoker : Java.Lang.Object, ICallback
{
IntPtr class_ref, id_onSuccess;
public ICallbackInvoker(IntPtr handle, JniHandleOwnership transfer) : base(handle, transfer)
{
IntPtr lref = JNIEnv.GetObjectClass(Handle);
class_ref = JNIEnv.NewGlobalRef(lref);
JNIEnv.DeleteLocalRef(lref);
}
protected override Type ThresholdType
{
get { return typeof(ICallbackInvoker); }
}
protected override IntPtr ThresholdClass
{
get { return class_ref; }
}
public void OnSuccess(string test)
{
if (id_onSuccess == IntPtr.Zero)
id_onSuccess = JNIEnv.GetMethodID(class_ref, "onSuccess", "(Ljava/lang/String;)V");
JNIEnv.CallVoidMethod(Handle, id_onSuccess, new JValue(new Java.Lang.String(test)));
}
protected override void Dispose(bool disposing)
{
if (class_ref != IntPtr.Zero)
JNIEnv.DeleteGlobalRef(class_ref);
class_ref = IntPtr.Zero;
base.Dispose(disposing);
}
}
}
答案 0 :(得分:1)
固定!一切都必须在同一名称空间