0
public class NewCallInfo
{
    public string Site { get; set; }
    public string CustomerName { get; set; }
    public string CustomerPhone { get; set; }
    public string CustomerExt { get; set; }
    public string CustomerAddress { get; set; }
    public string CustomerCity { get; set; }
    public string CustomerState { get; set; }
    public string CustomerZip { get; set; }
    public string ServiceType { get; set; }
    public string Priority { get; set; }
    public string Description { get; set; }
    public string Technician { get; set; }
    public string Serial { get; set; }
    public string Model { get; set; }
}

public interface INewCall
{
    NewCallInfo GetNewCallInfo(string site);
}

public class HandleNewCall : INewCall
{
   private SqLiteHelper Helper {get;set;}

   public HandleNewCall(Context context)
   {
      Helper = new SqLiteHelper(context);
   }
   public NewCallInfo GetNewCallInfo(string site)
   {
      string whereClause = string.Format("Where Site='{0}'", site);

        ICursor callCursor = _helper.ReadableDatabase.Query("NewCall", null, whereClause, null, null, null, null);
        NewCallInfo newCall = new NewCallInfo();

        while(callCursor.MoveToNext())
        {
            newCall.Site                =       callCursor.GetString(0);
            newCall.CustomerName        =       callCursor.GetString(1);
            newCall.CustomerPhone       =       callCursor.GetString(2);
            newCall.CustomerExt         =       callCursor.GetString(3);
            newCall.CustomerAddress     =       callCursor.GetString(4);
            newCall.CustomerCity        =       callCursor.GetString(5);
            newCall.CustomerState       =       callCursor.GetString(6);
            newCall.CustomerZip         =       callCursor.GetString(7);
            newCall.ServiceType         =       callCursor.GetString(8);
            newCall.Priority            =       callCursor.GetString(9);
            newCall.Description         =       callCursor.GetString(10);
            newCall.Technician          =       callCursor.GetString(11);
            newCall.Serial              =       callCursor.GetString(12);
            newCall.Model               =       callCursor.GetString(13);
        }

        if (string.IsNullOrEmpty(newCall.Site))
            newCall.Site = "None";

        return newCall;
   }
}

class MyApp : Application
{
    public INewCall NewCall { get; set; }

    public MyApp(IntPtr handle, JniHandleOwnership transfer)
        :base(handle, transfer)
    {

    }

    public override void OnCreate()
    {
       NewCall = new HandleNewCall(this);
    }
}

然后在我的活动中:

var call = ((MyApp) Application).NewCall.GetNewCallInfo("siteString");

我得到“无法从源类型转换为目标类型”。上面所有带有接口和应用程序子类的东西对我来说都是非常新的,但我觉得我所做的一切都和网上的例子一样。即在本例中。几个小时以来一直把我的头撞在墙上。有人看到我遗漏的任何明显的东西吗?

4

1 回答 1

1

您的类缺少正确生成 AndroidManifest.xml 的[Application]属性MyApp,因此 Android 不知道将其用作您的应用程序类。因此,尝试将Application属性强制转换MyApp为失败,因为该对象实际上不是您的类的实例。更新您的类定义,如下所示:

[Application]
public class MyApp : Application
{
    // ...
}
于 2012-07-07T00:07:02.557 回答