我正在使用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);
}
}
更新:
当我将它设为静态方法时,它可以工作,但我需要将一个实例传递给它才能使其工作。知道为什么静态方法会起作用吗?
答案 0 :(得分:0)
根据发布的代码,这应该有用。但是,如果你在不同的命名空间中有相同的类名,那么我之前遇到过这个问题......那就是正在使用的类名。