开发Android应用程式时程式码无法正常运作

时间:2018-12-12 11:15:07

标签: android

我生成了以下代码:

package com.example.bmi;

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


public class MainActivity extends AppCompatActivity {

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

    public void myButtonListenerMethod() {
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

    final EditText heightText = (EditText)
            findViewById(R.id.heightInput);
    String heightStr = heightText.getText().toString();
    double height = Double.parseDouble(heightStr);

    final EditText weightText = (EditText)
            findViewById(R.id.weightInput);
    String weightStr = weightText.getText().toString();
    double weight = Double.parseDouble(weightStr);

    double BMI = (weight)/(height*height);
    {
    final EditText BMIResult = (EditText)
            findViewById(R.id.BMIResult);
    BMIResult.setText(Double.toString(BMI));}

    String BMI_Cat;

            {
                if (BMI < 15) {
                    BMI_Cat = "Very severely underweight";
                } else if (BMI < 16) {
                    BMI_Cat = "Severely underweight";
                } else if (BMI < 18.5) {
                    BMI_Cat = "Underweight";
                } else if (BMI < 25) {
                    BMI_Cat = "Normal";
                } else if (BMI < 30) {
                    BMI_Cat = "Overweight";
                } else if (BMI < 35) {
                    BMI_Cat = "Obese Class 1 - Moderately Obese";
                } else if (BMI < 40) {
                    BMI_Cat = "Obese Class 2 - Severely Obese";
                } else {
                    BMI_Cat = "Obese Class 3 - Very Severely Obese";
                }
            }

        final TextView BMICategory = (TextView)
                findViewById(R.id.BMICategory);
        BMICategory.setText(BMI_Cat);
    }
            });
        }
    }

现在我遇到以下错误:

Error message shown in Android Studio

我的xml文件代码在这里:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minHeight="129dp"
    tools:context=".MainActivity"
    tools:layout_editor_absoluteY="81dp">

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="60dp"
        android:text="BMI Calculator"
        android:textSize="30sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="34dp"
        android:layout_marginLeft="34dp"
        android:layout_marginEnd="38dp"
        android:layout_marginRight="38dp"
        android:text="Enter your weight (kg):"
        android:textSize="18sp"
        app:layout_constraintBaseline_toBaselineOf="@+id/weightInput"
        app:layout_constraintEnd_toStartOf="@+id/weightInput"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="34dp"
        android:layout_marginLeft="34dp"
        android:layout_marginEnd="45dp"
        android:layout_marginRight="45dp"
        android:text="Enter your height (m):"
        android:textSize="18sp"
        app:layout_constraintBaseline_toBaselineOf="@+id/heightInput"
        app:layout_constraintEnd_toStartOf="@+id/heightInput"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="34dp"
        android:layout_marginLeft="34dp"
        android:layout_marginEnd="114dp"
        android:layout_marginRight="114dp"
        android:layout_marginBottom="56dp"
        android:text="Your BMI:"
        android:textSize="24sp"
        app:layout_constraintBottom_toTopOf="@+id/button"
        app:layout_constraintEnd_toStartOf="@+id/BMIResult"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/BMICategory"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="77dp"
        android:text="BMI category"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="36dp"
        android:text="Calculate My BMI"
        app:layout_constraintBottom_toTopOf="@+id/BMICategory"
        app:layout_constraintStart_toStartOf="@+id/BMICategory" />

    <EditText
        android:id="@+id/weightInput"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="154dp"
        android:layout_marginEnd="41dp"
        android:layout_marginRight="41dp"
        android:layout_marginBottom="155dp"
        android:ems="10"
        android:inputType="numberDecimal"
        android:text="80"
        app:layout_constraintBottom_toBottomOf="@+id/textView3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/heightInput"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="28dp"
        android:layout_marginEnd="41dp"
        android:layout_marginRight="41dp"
        android:ems="10"
        android:inputType="numberDecimal"
        android:text="1.80"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView2"
        app:layout_constraintTop_toBottomOf="@+id/weightInput" />

    <EditText
        android:id="@+id/BMIResult"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="45dp"
        android:layout_marginEnd="40dp"
        android:layout_marginRight="40dp"
        android:ems="10"
        android:inputType="numberDecimal"
        android:text="<><><><>"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/textView3"
        app:layout_constraintTop_toBottomOf="@+id/heightInput" />

</android.support.constraint.ConstraintLayout>

我需要帮助来解决此问题。我也尝试了其他源代码,但无法解决这些问题。因此,由于我是编程领域的新手,所以通过相关的可能示例对上述问题的解决将对我有所帮助。

预先感谢。

1 个答案:

答案 0 :(得分:0)

问题是error: not well-formed (invalid token). 原因是android:text="<><><><>" 解决方案是:<string name="name"><![CDATA[<><><><>]]></string> 在您的文本视图中传递