0

我正在使用 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);
       }
    }

更新:

当我将它设为静态方法时它可以工作,但我需要将一个实例传递给它才能工作。知道为什么静态方法会起作用吗?

4

1 回答 1

0

根据发布的代码,这应该可以工作。但是,如果您在不同的命名空间中具有相同的类名,我之前曾遇到过这种情况……这就是正在使用的类名。

于 2013-06-12T19:09:02.307 回答