2017-08-13 80 views
0

我有一個可移植的類庫從api檢索數據用於我正在用Xamarin開發的android應用程序。我只是從PCL中的任何方法獲得空值,我也無法調試可移植類庫。我創建了一個基本的類,其方法返回一個int來查看問題是否與我的api連接,但它仍然返回null,我一直無法弄清楚爲什麼會發生這種情況(這可能是非常明顯的) 。這是我返回int的代碼。Xamarin應用null從便攜式類庫返回

namespace TradeOffAndroidApp.Core.DataService 
{ 
    public class CategoryDataService 
    { 
     public int AInt() 
     { 
      return 111; 
     } 
    } 
} 

然後在我的活動我有

using Android.App; 
using Android.OS; 
using TradeOffAndroidApp.Core.DataService; 
namespace TradeOffAndroidApp 
{ 
    [Activity(Label = "CategoriesActivity")] 
    public class CategoriesActivity : Activity 
    { 
     private CategoryDataService testClass; 

     protected override void OnCreate(Bundle savedInstanceState) 
     { 
      base.OnCreate(savedInstanceState); 
      int a = 0; 
      a = testClass.AInt(); 
     } 
    } 
} 

INT上一個越來越引發的異常是

未處理的異常:

System.NullReferenceException:對象引用未設置爲對象的實例 。發生

回答

4

你必須創建一個類的實例,然後才能訪問它

testClasss = new CategoryDataService(); 
int a = testClass.AInt(); 
+0

感謝您和浪費你的時間在這樣一個顯而易見的問題對不起,我不知道我怎麼沒」沒有意識到。我會在需要的時間後標記爲已接受 – Kevin