Xamarin绑定Jar给出错误:没有实现继承的抽象成员AsyncTask.DoInBackground(params Object [])

时间:2017-05-08 01:49:01

标签: java xamarin android-asynctask xamarin.android

我想在我的Xamarin.Android项目中使用这个.jar文件:

https://drive.google.com/file/d/0B8xKHTqtwfKtdzVtdDQ5NEVIWlU/view?usp=sharing

在我创建绑定项目并尝试构建之后,我收到此错误:

obj\Debug\generated\src\Com.Acrcloud.Rec.Sdk.Utils.ACRCloudGetIPAddressAsyncTask.cs(23,23): Error CS0534: 'ACRCloudGetIPAddressAsyncTask' does not implement inherited abstract member 'AsyncTask.DoInBackground(params Object[])' (CS0534)

使用反编译器,我检查了ACRCloudGetIPAddressAsyncTask的内容并找到了:

package com.acrcloud.rec.sdk.utils;

import android.os.AsyncTask;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class ACRCloudGetIPAddressAsyncTask
extends AsyncTask<String, Integer, String>
{
    private static final String TAG = "ACRCloudAsynGetIPAddressTask";

    protected String doInBackground(String... params)
    {
        String ip = "";
        try
        {
            InetAddress x = InetAddress.getByName(params[0]);

            ip = x.getHostAddress();
        }
        catch (UnknownHostException e)
        {
            e.printStackTrace();
            ip = "";
        }
        catch (Exception e2)
        {
            ip = "";
        }
       return ip;
  }

  protected void onPostExecute(String ip)
  {
     super.onPreExecute();
     ACRCloudLogger.d("ACRCloudAsynGetIPAddressTask", ">>>>>>>>>>>>>>>  " + ip);
     com.acrcloud.rec.sdk.recognizer.ACRCloudRecognizerRemoteImpl.serverIP = ip;
  }
}

为什么我甚至需要实施DoInBackground(params Object[])?生成的C#代码实现protected virtual unsafe string DoInBackground (params string[] @params)以匹配Java代码中的内容...我对Java知之甚少,所以我可能遗漏了一些东西......任何想法如何我可以解决这个问题吗?

2 个答案:

答案 0 :(得分:9)

您可以查看这篇精彩帖子:Approaching a Xamarin.Android Bindings Case,通过查看Adding Types部分,当我们要添加类,更改构造函数或切换泛型类型时,它会使用<add-node>

对于您的lib,您可以打开Metadata.xml文件夹下的Transforms,然后添加以下代码:

<add-node path="/api/package[@name='com.acrcloud.rec.sdk.utils']">
  <class abstract="false" deprecated="not deprecated" final="false" name="ACRCloudLocalRecognizerInitAsyncTask" static="true" visibility="public" extends="java.lang.Object">
  </class>

  <class abstract="false" deprecated="not deprecated" final="false" name="ACRCloudGetIPAddressAsyncTask" static="true" visibility="public" extends="java.lang.Object">
  </class>
</add-node>

然后它可以成功编译,但我没有测试将此lib引用到Xamarin.Android项目,您可能需要进一步研究使用此lib。无论如何,这篇文章有很多帮助。

您还可以在SO上查看类似案例:Java Binding Abstract class not being generated

答案 1 :(得分:0)

<add-node path="/api/package[@name='yourpPackageName']">
   <class abstract="false" deprecated="not deprecated" final="false" name="YourClassName" static="true" visibility="public" extends="java.lang.Object"> </class>