继承Xamarin Monodroid中的FileInputStream

时间:2012-12-04 21:34:48

标签: xamarin.android xamarin

我有一个继承自Java.IO.FileInputStream的类。它看起来像这样:

public class DeviceInputStream : FileInputStream
{
    private FileDescriptor descriptor = null;
    private byte currentValue;

    public DeviceInputStream(FileDescriptor fd) : base(fd)
    {
        descriptor = fd;
    }

    public DeviceInputStream(File file) : base(file){}

    public DeviceInputStream(string fileName):base(fileName){}

    public override int Read()
    {
        int byteRead = base.Read();
        currentValue = (byte) byteRead;
        return byteRead;
    }

    public byte CurrentValue
    {
        get { return currentValue; }
    }
}

但是,当我编译它时,我收到以下错误:

未报告的异常FileNotFoundException;必须被抓住或宣布被抛出         超(p0);     DeviceInputStream.java:20

关于可能导致此问题的任何想法?感谢。

-Shaun

1 个答案:

答案 0 :(得分:0)

这似乎是Xamarin Monodroid的一个问题,并且已被提交为将ExportAttribute扩展为构造函数的bug。详情来自:

http://forums.xamarin.com/discussion/500/inheriting-from-fileinputstream

在构建时,为每个Java.Lang.Object子类生成Android Callable Wrappers(ACW),其中包括我们的DeviceInputStream类型:

public class DeviceInputStream
extends java.io.FileInputStream
implements
    mono.android.IGCUserPeer
{
static final String __md_methods;
static {
    __md_methods = 
        "n_read:()I:GetReadHandler\n" +
        "";
    mono.android.Runtime.register ("Scratch.ContentProvidersHateApplications.DeviceInputStream, Scratch.ContentProvidersHateApplications, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", DeviceInputStream.class, __md_methods);
}


public DeviceInputStream (java.io.File p0)
{
    super (p0);
    if (getClass () == DeviceInputStream.class)
        mono.android.TypeManager.Activate ("Scratch.ContentProvidersHateApplications.DeviceInputStream, Scratch.ContentProvidersHateApplications, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "Java.IO.File, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=c4c4237547e4b6cd", this, new java.lang.Object[] { p0 });
}


public DeviceInputStream (java.io.FileDescriptor p0)
{
    super (p0);
    if (getClass () == DeviceInputStream.class)
        mono.android.TypeManager.Activate ("Scratch.ContentProvidersHateApplications.DeviceInputStream, Scratch.ContentProvidersHateApplications, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "Java.IO.FileDescriptor, Mono.Android, Version=0.0.0.0, Culture=neutral, PublicKeyToken=c4c4237547e4b6cd", this, new java.lang.Object[] { p0 });
}


public DeviceInputStream (java.lang.String p0)
{
    super (p0);
    if (getClass () == DeviceInputStream.class)
        mono.android.TypeManager.Activate ("Scratch.ContentProvidersHateApplications.DeviceInputStream, Scratch.ContentProvidersHateApplications, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "System.String, mscorlib, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e", this, new java.lang.Object[] { p0 });
}


    public int read ()
    {
        return n_read ();
    }

    private native int n_read ();

/* ... */
}

由于ACW是Java代码,因此它们必须是有效的Java代码。不幸的是,在这种情况下它们不是,因为例如超级(p0)调用。 DeviceInputStream(String)构造函数是FileInputStream(String),它抛出FileNotFoundException。

由于DeviceInputStream(String)构造函数调用可能抛出异常的方法,因此DeviceInputStream(String)必须包含throws子句或在super(p0)语句周围有try / catch块。两者都没有发生,因此编译错误。

不幸的是,目前还没有解决方法;没有办法在构造函数体上向ACW添加抛出,并且没有其他方法可以自定义ACW的构造函数,所以你就被卡住了。 : - (

支持扩展ExportAttribute,以便可以在bug 8754提交。