xamarin android构建错误:“无法为类创建JavaTypeInfo" [英] xamarin android build error: "Failed to create JavaTypeInfo for class"

查看:94
本文介绍了xamarin android构建错误:“无法为类创建JavaTypeInfo"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在构建时,编译器将引发以下错误:

On build, the compiler is throwing the following error:

错误无法为类创建JavaTypeInfo:App.Droid.Controls.WebViewJavaScriptInterface由于System.NullReferenceException:对象引用未设置为实例一个对象.在Java.Interop.Tools.JavaCallableWrappers.JavaCallableWrapperGenerator.Signature..ctor(字符串名称,字符串签名,字符串连接器,字符串managedParameters,字符串externalType,字符串superCall)Java.Interop.Tools.JavaCallableWrappers.JavaCallableWrapperGenerator.Signature..ctor(MethodDefinition方法,ExportAttribute导出)位于Java.Interop.Tools.JavaCallableWrappers.JavaCallableWrapperGenerator.AddMethod(MethodDefinition已注册方法,已实现MethodDefinition)Java.Interop.Tools.JavaCallableWrappers.JavaCallableWrapperGenerator..ctor(TypeDefinition类型,字符串externalType,Action2日志)在Java.Interop.Tools.JavaCallableWrappers.JavaCallableWrapperGenerator..ctor(TypeDefinition类型,Action2日志)在Xamarin.Android.Tasks.Generator.GenerateJavaSource(TaskLoggingHelper日志,TypeDefinition t,字符串outputPath,字符串applicationJavaClass,布尔值useSharedRuntime,布尔值generateOnCreateOverrides,布尔值hasExportReference)

Error Failed to create JavaTypeInfo for class: App.Droid.Controls.WebViewJavaScriptInterface due to System.NullReferenceException: Object reference not set to an instance of an object. at Java.Interop.Tools.JavaCallableWrappers.JavaCallableWrapperGenerator.Signature..ctor(String name, String signature, String connector, String managedParameters, String outerType, String superCall) at Java.Interop.Tools.JavaCallableWrappers.JavaCallableWrapperGenerator.Signature..ctor(MethodDefinition method, ExportAttribute export) at Java.Interop.Tools.JavaCallableWrappers.JavaCallableWrapperGenerator.AddMethod(MethodDefinition registeredMethod, MethodDefinition implementedMethod) at Java.Interop.Tools.JavaCallableWrappers.JavaCallableWrapperGenerator..ctor(TypeDefinition type, String outerType, Action2 log) at Java.Interop.Tools.JavaCallableWrappers.JavaCallableWrapperGenerator..ctor(TypeDefinition type, Action2 log) at Xamarin.Android.Tasks.Generator.GenerateJavaSource(TaskLoggingHelper log, TypeDefinition t, String outputPath, String applicationJavaClass, Boolean useSharedRuntime, Boolean generateOnCreateOverrides, Boolean hasExportReference)

我为Webview创建了一个自定义渲染器,我正在尝试在其中注入JavaScriptInterface.我有一个针对不同项目的解决方案,这也许是导致上述问题的原因,也许不是.

I have created a custom renderer for the webview, where I am trying to inject JavaScriptInterface. I have a solution with different Projects, which might be reason for the above issue, or maybe not.

public class WebviewRendererEX : WebViewRenderer
{
    Context _context;

    public WebviewRendererEX(Context context) : base(context)
    {
        _context = context;
    }
    protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
    {
        base.OnElementChanged(e);
        if (Control != null)
        {
            CookieManager cm = CookieManager.Instance;
            cm.SetAcceptCookie(true);
            cm.SetAcceptThirdPartyCookies(Control, true);
            Control.Settings.JavaScriptEnabled = true;
            Control.Settings.DomStorageEnabled = true;
            Control.AddJavascriptInterface(this, "Android");
            Device.BeginInvokeOnMainThread(() =>
            {
                Control.EvaluateJavascript("function someNavigate(dict){Android.navigateTo(dict);}", null);
            });    
        }
    }
}

public class WebViewJavaScriptInterface : Java.Lang.Object
{

    private Context context;
    public WebViewJavaScriptInterface(Context context)
    {
        this.context = context;
    }
    [Java.Interop.Export("navigateTo")]
    [JavascriptInterface]
    public void NavigateTo(Dictionary<string, object> dict)
    {
        Console.WriteLine(dict);
    }
}

我希望该应用程序可以正常运行,并且在webview加载javascriptInterface后即可正常工作.

I expected that the App should run without any complietime issue, And once webview loaded javascriptInterface should work.

推荐答案

根据您的构建器错误, ExportAttribute 用于指示"Java代码生成器导出成为Android的Java方法.可调用包装(ACW)"和 Dictionary< string,object> 不是Java对象(duh),并且Java代码生成器不知道如何处理它.

Based on your builder error, the ExportAttribute is used to instruct the "Java code generator to export a Java method that becomes an Android Callable Wrapper (ACW)" and Dictionary<string, object> is not a Java object (duh) and the Java code generator has no idea how to handle it.

[Java.Interop.Export("navigateTo")]
[JavascriptInterface]
public void NavigateTo(Dictionary<string, object> dict)
{
   Console.WriteLine(dict);
}

因此,解决此问题的简单方法是将参数类型从 Dictionary< string,object> 切换为 Java.Lang.Object .现在,Java代码生成器可以正确生成ACW,并且编译成功.

So the simple fix to this problem was to switch the parameter type from Dictionary<string, object> to Java.Lang.Object. Now the Java code generator can properly generate an ACW and the compilation succeeds.

[Java.Interop.Export("navigateTo")]
[JavascriptInterface]
public void NavigateTo(Java.Lang.String dict)
{
    Console.WriteLine(dict);
}

这篇关于xamarin android构建错误:“无法为类创建JavaTypeInfo"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆