2013-06-12 102 views
0

我正在建造一個使用C夏普(與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); 
     } 
    } 

UPDATE:

它工作時,我讓一個靜態方法,但我需要一個實例傳遞給它的是工作。任何想法爲什麼靜態方法會起作用?

+0

聖地獄Xamarin看起來非常甜。我不知道它存在。你喜歡它嗎? – Jonesopolis

+1

是的,它非常完整,基本上可以滿足您的一切需求。在使用Java for Android一段時間之後,它有點過渡了,有時會出現一些小錯誤,但值得平衡使用C#並能夠在iOS中重用大量代碼。 – clifgray

回答

0

根據發佈的代碼應該工作。然而,如果在不同的命名空間中有相同的類名,那麼我曾經遇到過這種情況......而且這是正在使用的類。

+0

嗯沒關係那裏沒有衝突。它必須是與Xamarin – clifgray

+0

特定的東西,它與靜態方法,它實際上不符合我所需要的,因爲我需要一個實例,但任何想法,爲什麼會工作? – clifgray

+0

我想不出任何理由,因爲你用一個實例調用它。除非該實例正在被讀爲一種類型? –

相關問題