RadioButton无效的双倍

时间:2016-02-04 16:53:37

标签: java android xml

我想要5个RadioButton,所有这些都在XML文件中用文本描述。我希望它们保持double值,每个按钮都不同。 并且用户必须输入一个数字,然后在RadioButtons中选择五个选项中的一个,然后按常规按钮,该按钮将进行计算(将输入的数字乘以RadioButton保存的双号)。

我得到强制关闭错误:

java.lang.NumberFormatException: Invalid double: "Sjedeći (Malo ili bez aktivnosti, pretežno sjedeći posao)"

这是我的java文件:

package hr.app.liftme.liftmehr;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;

public class KalkulatoriTDEE extends AppCompatActivity {

    EditText unesiteBMR;
    RadioGroup radioGrupa;
    RadioButton radioBtn1, radioBtn2, radioBtn3, radioBtn4, radioBtn5;
    Button btnIzracunajTDEE;
    TextView textRezultatTDEE;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kalkulatori_tdee);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        Intent intent = getIntent();


        unesiteBMR = (EditText) findViewById(R.id.editUnesiteBMR);
        radioGrupa = (RadioGroup) findViewById(R.id.radioGrupa);
        radioBtn1 = (RadioButton) findViewById(R.id.radioBtn1);
        radioBtn2 = (RadioButton) findViewById(R.id.radioBtn2);
        radioBtn3 = (RadioButton) findViewById(R.id.radioBtn3);
        radioBtn4 = (RadioButton) findViewById(R.id.radioBtn4);
        radioBtn5 = (RadioButton) findViewById(R.id.radioBtn5);
        btnIzracunajTDEE = (Button) findViewById(R.id.btnIzracunajTDEE);
        textRezultatTDEE = (TextView) findViewById(R.id.textRezultatTDEE);

        btnIzracunajTDEE.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                double prviButton = Double.parseDouble(radioBtn1.getText().toString());
                double drugiButton = Double.parseDouble(radioBtn2.getText().toString());
                double treciButton = Double.parseDouble(radioBtn3.getText().toString());
                double cetvrtiButton = Double.parseDouble(radioBtn4.getText().toString());
                double petiButton = Double.parseDouble(radioBtn5.getText().toString());



                prviButton = 1.2;
                drugiButton = 1.375;
                treciButton = 1.55;
                cetvrtiButton = 1.725;
                petiButton = 1.9;

                double rezultat = 0;
                double input = 0;

                if (radioBtn1.isChecked()){
                    rezultat = input * prviButton;
                } else if(radioBtn2.isChecked()){
                    rezultat = input * drugiButton;
                } else if (radioBtn3.isChecked()){
                    rezultat = input * treciButton;
                } else if (radioBtn4.isChecked()) {
                    rezultat = input * cetvrtiButton;
                } else if (radioBtn5.isChecked()) {
                    rezultat = input * petiButton;
                }
                textRezultatTDEE.setText(Double.toString(rezultat));
            }
        });

    }
}

这是XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="hr.app.liftme.liftmehr.KalkulatoriTDEE"
    tools:showIn="@layout/activity_kalkulatori_tdee">


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/linearLayout22"
        android:layout_marginBottom="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Unesite svoj BMR:"
            android:id="@+id/textView115" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="numberDecimal"
            android:ems="10"
            android:id="@+id/editUnesiteBMR" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout22"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/linearLayout23"
        android:layout_marginBottom="10dp">

        <RadioGroup
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/radioGrupa">

            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Sjedeći (Malo ili bez aktivnosti, pretežno sjedeći posao)"
                android:id="@+id/radioBtn1"/>


            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Laka aktivnosti (Lake vježbe/sport 1-3 dana sedmično)"
                android:id="@+id/radioBtn2"/>



            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Umjeren (Umjerene vježbe/sport 3-5 dana sedmično)"
                android:id="@+id/radioBtn3"/>


            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Teške aktivnosti (Teže vježbe/sport 6-7 dana sedmično)"
                android:id="@+id/radioBtn4"/>

            <RadioButton
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Ekstremne aktivnosti (Teške dnevne vježbe/sport i fizički posao 2 x dnevno)"
                android:id="@+id/radioBtn5"/>



        </RadioGroup>

    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/linearLayout23">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Izračunaj"
            android:id="@+id/btnIzracunajTDEE"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="10dp"
            android:background="#424242"
            android:textColor="#ffffff" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="Vaš TDEE je:"
            android:id="@+id/textView116"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="5dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:id="@+id/textRezultatTDEE"
            android:layout_gravity="center_horizontal" />

    </LinearLayout>

</RelativeLayout>

我错过了什么?

2 个答案:

答案 0 :(得分:1)

删除以下内容,尝试将您的radiobuttons文本转换为double,并导致崩溃:

double prviButton = Double.parseDouble(radioBtn1.getText().toString());
double drugiButton = Double.parseDouble(radioBtn2.getText().toString());
double treciButton = Double.parseDouble(radioBtn3.getText().toString());
double cetvrtiButton = Double.parseDouble(radioBtn4.getText().toString());
double petiButton = Double.parseDouble(radioBtn5.getText().toString());

相反,只需使用他们应具有的值初始化您的double字段,如下所示:

double prviButton = 1.2;
double drugiButton = 1.375;
double treciButton = 1.55;
double cetvrtiButton = 1.725;
double petiButton = 1.9;

答案 1 :(得分:0)

解决!

btnIzracunajTDEE.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){

                double input = Double.parseDouble(unesiteBMR.getText().toString());
               double prviButton = 1.2;
                double drugiButton = 1.375;
                double treciButton = 1.55;
                double cetvrtiButton = 1.725;
                double petiButton = 1.9;

                double rezultat = 0;


                if (radioBtn1.isChecked()){
                    rezultat = input * prviButton;
                } else if(radioBtn2.isChecked()){
                    rezultat = input * drugiButton;
                } else if (radioBtn3.isChecked()){
                    rezultat = input * treciButton;
                } else if (radioBtn4.isChecked()) {
                    rezultat = input * cetvrtiButton;
                } else if (radioBtn5.isChecked()) {
                    rezultat = input * petiButton;
                }
                textRezultatTDEE.setText(Double.toString(rezultat));
            }
        });

    }
}