简体   繁体   English

我编写的第一个应用程序崩溃了,我也不知道为什么

[英]The first app I have ever written crashes and I don't know why

I've started using Xamarin to program android apps in C#, but my app crashes before it even does anything. 我已经开始使用Xamarin在C#中编写android应用程序,但是我的应用程序在执行任何操作之前就崩溃了。 Can anyone identify why? 谁能确定原因?

The purpose of the app is to simply take a quadratic equation in the form ax^2+bx+c and find it's two solutions and then display those solutions. 该应用程序的目的是简单地采用ax ^ 2 + bx + c形式的二次方程式,找到它的两个解,然后显示这些解。

Thanks. 谢谢。

[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));
        }
    }
}

I think the following lines are attempting to convert an undefined int to a string: 我认为以下几行正在尝试将未定义的int转换为字符串:

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

In my testing locally the project would not build until s1 and s2 had values. 在我本地测试中,只有在s1s2具有值之前,该项目才能构建。

int s1, s2 = -1;

I found the solution! 我找到了解决方案! Simply put, I apparently had started developing my app for android API Level 22, and was trying to run it on a Level 21 emulator, so it wouldn't cope with it. 简而言之,我显然已经开始为Android API Level 22开发我的应用,并试图在Level 21仿真器上运行它,因此它无法应对。

Downloading the Level 22 image and making a new emulator fixed the issue. 下载22级映像并制作新的仿真器可以解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM