矩形计算器应用程序

时间:2017-02-14 09:32:27

标签: java android

我想知道为什么我的计算器应用程序上的输出会给我这个消息?

message

我似乎总是看起来很简单。我认为可能是输出错误的EditTexts或解析我不需要的东西。除了将数字限制为两位小数,我可以找到一个修复,但我想我还是会问。任何提示都会受到赞赏,对于android编程来说还是一个新手。

    package com.filej.calculatorapp;
    import android.support.v4.media.MediaBrowserServiceCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    public class CalculatorApp extends AppCompatActivity` {

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

        EditText heightEdit = (EditText) findViewById(R.id.heightEdit);
        EditText widthEdit = (EditText) findViewById(R.id.widthEdit);
        TextView area = (TextView) findViewById(R.id.areaTextView);
        TextView perimeter = (TextView) findViewById(R.id.perimeterTextView);

        Button calculate = (Button) findViewById(R.id.calculateButton);

        View view = findViewById(R.id.calculateButton);
        view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText height = (EditText) findViewById(R.id.heightEdit);
                EditText width = (EditText) findViewById(R.id.widthEdit);
                calculateArea(height.getText().toString(), width.getText().toString());
                calculatePerimeter(height.getText().toString(), width.getText().toString());

            }
        });


    }

    private void calculateArea(String cHeight, String cWidth)
    {
        TextView area = (TextView) findViewById(R.id.areaTextView);
        int area1 = Integer.parseInt(Height)* Integer.parseInt(Width);
        area.setText(String.valueOf(area));
    }
    private void calculatePerimeter (String dHeight, String dWidth)
    {
        TextView perimeter = (TextView) findViewById(R.id.perimeterTextView);
        int perimeter1 = (Integer.parseInt(Height) * 2) + (Integer.parseInt(Width) * 2);
        perimeter.setText(String.valueOf(perimeter));
    }
    }
  

这是我的XML

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_calculator_app"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.filej.calculatorapp.CalculatorApp">

    <TextView
        android:text="Width"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="33dp"
        android:id="@+id/textView2" />

    <TextView
        android:text="Height"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:id="@+id/textView"
        android:layout_below="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:text="Area"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="81dp"
        android:id="@+id/textView3"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="41dp"
        android:id="@+id/textView4"
        android:layout_below="@+id/textView3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:text="Perimeter" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number|numberDecimal"
        android:ems="10"
        android:id="@+id/widthEdit"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:singleLine="false"
        android:text="Enter Width" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="number|numberDecimal"
        android:ems="10"
        android:id="@+id/heightEdit"
        android:text="Enter Height"
        android:layout_alignBaseline="@+id/textView"
        android:layout_alignBottom="@+id/textView"
        android:layout_alignLeft="@+id/widthEdit"
        android:layout_alignStart="@+id/widthEdit" />

    <TextView
        android:text="Total Area"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/textView3"
        android:layout_alignLeft="@+id/heightEdit"
        android:layout_alignStart="@+id/heightEdit"
        android:id="@+id/areaTextView"
        android:layout_alignRight="@+id/heightEdit"
        android:layout_alignEnd="@+id/heightEdit" />

    <TextView
        android:text="Total Perimeter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/perimeterTextView"
        android:layout_alignBaseline="@+id/textView4"
        android:layout_alignBottom="@+id/textView4"
        android:layout_alignLeft="@+id/areaTextView"
        android:layout_alignStart="@+id/areaTextView"
        android:layout_alignRight="@+id/areaTextView"
        android:layout_alignEnd="@+id/areaTextView" />

    <Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView4"
        android:layout_toRightOf="@+id/textView2"
        android:layout_toEndOf="@+id/textView2"
        android:layout_marginTop="52dp"
        android:id="@+id/calculateButton" />

</RelativeLayout>

3 个答案:

答案 0 :(得分:3)

你的听众不应该在你的计算按钮而不是那个视图吗?像这样的东西

calculate = (Button) findViewById(R.id.calculateButton);
            calculate.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    EditText height = (EditText) findViewById(R.id.heightEdit);
                    EditText width = (EditText) findViewById(R.id.widthEdit);
                    calculateArea(height.getText().toString(), width.getText().toString());
                    calculatePerimeter(height.getText().toString(), width.getText().toString());

                }
            });

并且您还要使用错误的值设置textview并解析错误的变量,请尝试:

private void calculateArea(String cHeight, String cWidth)
{
    TextView area = (TextView) findViewById(R.id.areaTextView);
    int area1 = Integer.parseInt(cHeight)* Integer.parseInt(cWidth);
    area.setText(String.valueOf(area1));
}
private void calculatePerimeter (String dHeight, String dWidth)
{
    TextView perimeter = (TextView) findViewById(R.id.perimeterTextView);
    int perimeter1 = (Integer.parseInt(dHeight) * 2) + (Integer.parseInt(dWidth) * 2);
    perimeter.setText(String.valueOf(perimeter1));
}

答案 1 :(得分:1)

您的行area.setText(String.valueOf(area));正在将文本设置为对象的名称。

答案 2 :(得分:1)

您为编辑文本设置了错误的文字

使用

  area.setText(String.valueOf(area1));

 perimeter.setText(String.valueOf(perimeter1));

而不是

  area.setText(String.valueOf(area));
  perimeter.setText(String.valueOf(perimeter));

在您的案例区域和周边是视图,area1和perimeter1是计算值