Java Android应用程序在调用AddOperator方法时崩溃,未显示任何错误

时间:2018-12-19 20:02:53

标签: java c# android android-studio

关于xml,它工作得很好+所有按钮都调用ButtonClick方法

还有50/50的机会根本无法启动(引导)

Java:

package com.example.MYNAME.javacalchopefullygo;


import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private TextView calculatorText;

private String[] numbers = new String[2];
private String operator;

public MainActivity() {

}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    calculatorText = findViewById(R.id.calculator_text_view);
}

public void ButtonClick(View view) {
    Button button = (Button) view;
    String buttontextstring = "" + button.getText();

    if ("0123456789.".contains(buttontextstring))
        AddDigitOrDecimalPoint(buttontextstring);

    else if ("÷×+-".contains(buttontextstring))
        AddOperator(buttontextstring);

    else if ("=".equals(buttontextstring))
        Calculate(null);

    else
        Erase();
}

private void AddDigitOrDecimalPoint(String value) {
    int index = operator.equals(null) ? 0 : 1;

    if (value.equals(".") && numbers[index].contains("."))
        return;

    numbers[index] += value;

    UpdateCalculatorText();
}

private void AddOperator(String value) {
    if (!(numbers[1].equals(null))) {
        Calculate(value);
        return;
    }

    operator = value;

    UpdateCalculatorText();
}

private void Calculate(String newOperator) {
    Double result = null;
    Double first = numbers[0].equals(null) ? null : (Double) 
Double.parseDouble(numbers[0]);
    Double second = numbers[1].equals(null) ? null : (Double) 
Double.parseDouble(numbers[1]);

    switch (operator) {
        case "÷":
            result = first / second;
            break;
        case "×":
            result = first * second;
            break;
        case "+":
            result = first + second;
            break;
        case "-":
            result = first - second;
            break;
    }

    if (result != null) {
        numbers[0] = "" + result;
        operator = newOperator;
        numbers[1] = null;
        UpdateCalculatorText();
    }
}

private void Erase() {
    numbers[0] = numbers[1] = null;
    operator = null;
    UpdateCalculatorText();
}

private void UpdateCalculatorText() {
    calculatorText.setText(numbers[0] + " " + operator + " " + 
numbers[1]);
    //calculatorText.setText("{numbers[0]} {@operator} {numbers[1]}");
}
}

以下是我的原始C#代码,我将其“转换”为Java

using Android.App;
using Android.OS;
using Android.Views;
using Android.Widget;
using System;
using System.Data;

namespace CalcArray
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", 
MainLauncher = true)]
public class MainActivity : Activity
{
    private TextView calculatortext;

    private string[] numbers = new string[2];
    private string @operator;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);

        calculatortext = FindViewById<TextView> 
(Resource.Id.textViewResult);
    }

    [Java.Interop.Export("ButtonClick")]
    public void ButtonClick(View v)
    {
        Button button = (Button)v;

        if ("0123456789".Contains(button.Text))
        {
            AddDigitOrDecimalPoint(button.Text);
        }
        else if ("÷×+-".Contains(button.Text))
        {
            AddOperator(button.Text);
        }
        else if ("=" == button.Text)
        {
            Calculate();
        }
        else if ("CE" == button.Text)
        {
            EraseEntry();
        }
        else
        {
            Erase();
        }
    }

    private void EraseEntry()
    {
        string temp = calculatortext.Text;

        temp = temp.Remove(temp.Length - 1);

        UpdateCalculatorText(temp);
    }

    private void Erase()
    {
        numbers[0] = numbers[1] = null;
        @operator = null;
        UpdateCalculatorText(null);
    }

    private void Calculate(string newOperator = null)
    {
        double? result = null;
        double? first = numbers[0] == null ? null : 
(double?)double.Parse(numbers[0]);
        double? second = numbers[1] == null ? null : 
(double?)double.Parse(numbers[1]);

        switch (@operator)
        {
            case "÷":
                result = first / second;
                break;
            case "×":
                result = first * second;
                break;
            case "+":
                result = first + second;
                break;
            case "-":
                result = first - second;
                break;
        }

        if (result != null)
        {
            numbers[0] = result.ToString();
            @operator = newOperator;
            numbers[1] = null;
            UpdateCalculatorText(null);
        }
    }

    private void AddOperator(string value)
    {
        if (numbers[1] != null)
        {
            Calculate(value);
            return;
        }
        else
        {
            @operator = value;

            UpdateCalculatorText(null);
        }
    }

    private void AddDigitOrDecimalPoint(string value)
    {
        int index = @operator == null ? 0 : 1;

        if (value == "." && numbers[index].Contains("."))
            return;

        numbers[index] += value;

        UpdateCalculatorText(null);
    }

    private void UpdateCalculatorText(string value)
    {
        if (value != null)
        {
            calculatortext.Text = value;
            numbers[1] = null;
        }
        else
        {
            calculatortext.Text = $"{ numbers[0]} {@operator} 
{numbers[1]}";
        }
    }
}
}

是的,抱歉,代码格式很丑陋。一百万次按空格键或选项卡按钮不是我的事:)

0 个答案:

没有答案