【发布时间】:2013-06-12 19:06:25
【问题描述】:
我正在使用 C Sharp(使用 Xamarin)构建一个 android 应用程序,并且我试图从当前名为 FeedActivity.cs 的另一个类中调用一个方法,但我遇到了一个相当普遍的错误:
"FeedActivity:PostPicture: Exception of type 'Java.Lang.RuntimeException' was thrown."
这是一个 Java 错误,因为 Xamarin 将 C# 编译为 Java,以便它可以在 Android 上运行。
代码如下:
#class from which I want to call the method from the FacebookActivity.cs class
public class FeedActivity : BaseActivity
{
#the method that actually starts the whole process
private void PostPicture(pic picture, bool shareOnFacebook)
{
#posts the picture to my own app, no problem here
#try catch statement, this is where the error comes in
try
{
if (shareOnFacebook) SharePictureOnFacebook(picture);
}
catch (Exception ex)
{
LogManager.Error(this, ex, "FeedActivity:PostPicture: ", ex.Message);
}
}
#method from which I want to call the method from the FacebookActivity.cs class
private void SharePictureOnFacebook(pic picture)
{
FacebookActivity permissionAdder = new FacebookActivity();
#this is the line that is giving me the error
permissionAdder.AddPermissions()
#some more code that isn't relevant
}
}
#class that I want to use the method from
public class FacebookActivity : BaseActivity
{
#method I want to call from FeedActivity.cs in the SharePictureOnFacebook method
public void AddPermissions()
{
Authorize(this, AdditionalPermissions);
}
}
更新:
当我将它设为静态方法时它可以工作,但我需要将一个实例传递给它才能工作。知道为什么静态方法会起作用吗?
【问题讨论】:
-
神圣的 Xamarin 看起来真的很可爱。我不知道它存在。你喜欢吗?
-
是的,它非常完整,基本上拥有您可能需要的一切。在为 Android 使用 Java 一段时间后,这是一个小小的转变,有时会出现一些小错误,但使用 C# 并能够为 iOS 重用大量代码是值得的。