我正在使用 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);
}
}
更新:
当我将它设为静态方法时它可以工作,但我需要将一个实例传递给它才能工作。知道为什么静态方法会起作用吗?