C# 我写的第一个应用程序崩溃了,我不知道';我不知道为什么

C# 我写的第一个应用程序崩溃了,我不知道';我不知道为什么,c#,android,xamarin,C#,Android,Xamarin,我已经开始使用Xamarin在C#中编写android应用程序,但我的应用程序甚至在做任何事情之前就崩溃了。有人知道为什么吗 应用程序的目的是简单地取一个形式为ax^2+bx+c的二次方程,找到它的两个解,然后显示这些解 谢谢 [Activity (Label = "FirstApp", MainLauncher = true, Icon = "@drawable/icon")] public class MainActivity : Activity { int a = 4;

我已经开始使用Xamarin在C#中编写android应用程序,但我的应用程序甚至在做任何事情之前就崩溃了。有人知道为什么吗

应用程序的目的是简单地取一个形式为ax^2+bx+c的二次方程,找到它的两个解,然后显示这些解

谢谢

[Activity (Label = "FirstApp", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
    int a = 4;
    int b = 1 ;
    int c = 2;
    int d;
    int s1;
    int s2;
    string fail ="There are no real roots!";


    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        // Get our button from the layout resource,
        // and attach an event to it
        Button button = FindViewById<Button> (Resource.Id.myButton);
        TextView sol2 = FindViewById<TextView> (Resource.Id.Solution2);
        TextView sol1 = FindViewById<TextView> (Resource.Id.Solution1);

        button.Click += delegate {
            quadratic(a,b,c);

            sol1.Text = string.Format (Convert.ToString(s1));

            sol2.Text = string.Format (Convert.ToString(s2));
        };

    }
    string quadratic (int a, int b, int c)
    {
        d = Convert.ToInt32(Math.Pow (b, 2)) - (4 * a * c);
        if (d < 0) {
            return(fail);
        } else{
            s1 = (-1 * b + Convert.ToInt32(Math.Sqrt (d))) / (2 * a);
            s2 = (-1 * b - Convert.ToInt32(Math.Sqrt (d))) / (2 * a);
            return(Convert.ToString(s1));
        }
    }
}
[活动(Label=“FirstApp”,MainLauncher=true,Icon=“@drawable/Icon”)]
公共课活动:活动
{
INTA=4;
int b=1;
int c=2;
int d;
int s1;
int s2;
string fail=“没有真正的根!”;
创建时受保护的覆盖无效(捆绑包)
{
base.OnCreate(bundle);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.Main);
//从布局资源中获取我们的按钮,
//并在其上附加一个事件
Button Button=FindViewById(Resource.Id.myButton);
TextView sol2=findviewbyd(Resource.Id.Solution2);
TextView sol1=findviewbyd(Resource.Id.Solution1);
按钮。单击+=委派{
二次(a,b,c);
sol1.Text=string.Format(Convert.ToString(s1));
sol2.Text=string.Format(Convert.ToString(s2));
};
}
字符串二次型(inta,intb,intc)
{
d=转换为32(数学功率(b,2))-(4*a*c);
if(d<0){
返回(失败);
}否则{
s1=(-1*b+转换为32(Math.Sqrt(d))/(2*a);
s2=(-1*b-转换为32(Math.Sqrt(d))/(2*a);
返回(转换为字符串(s1));
}
}
}

我认为以下几行正在尝试将未定义的int转换为字符串:

sol1.Text = string.Format (Convert.ToString(s1));
sol2.Text = string.Format (Convert.ToString(s2));
在我的本地测试中,只有s1s2具有值,项目才会构建

int s1, s2 = -1;
我找到了解决办法! 简单地说,我显然已经开始为android API Level 22开发我的应用程序,并试图在Level 21模拟器上运行它,所以它无法应对


下载Level 22映像并制作一个新的仿真器解决了这个问题。

检查您的logcat和/或控制台输出。它会告诉你是什么导致了崩溃。这不是问题所在,但你真的应该避免私有成员字段和局部变量同名。我通常在我的成员字段前面加一个下划线“\u1”,这样可以很容易地找到它们并将其识别为类成员字段。此外,请尝试一些基本的调试。把一切都说出来。然后逐行取消注释,直到找到触发错误的原因。然后谷歌,或者在这里发布更多细节。可能是您的一个视图(
按钮
文本视图
)。尝试注释它们(以及所有相关代码),然后再次运行该应用程序……C#int是值类型,不能未定义。即使s1和s2可为空,它们的默认值也为0,
Convert.ToString
也能很好地处理这个问题。