2016-02-04 2 views
1

Я хочу иметь 5 RadioButtons, и все они описаны в тексте в XML-файле. И я хочу, чтобы они держали значение double, другое для каждой кнопки. И пользователь должен ввести одно число, а затем выбрать один из пяти вариантов, доступных в RadioButtons, затем нажать на обычную кнопку, и эта кнопка выполнит расчет (умножьте введенный номер с двойным номером, удерживаемым RadioButton).RadioButton invalid double

Я получаю ошибку принудительное закрытие:

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> 

Что мне не хватает?

ответ

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; 
+0

Да, только после того, как я разместил вопрос. Кнопка теперь работает, но результат всегда равен 0, кажется, что он не принимает значение ввода пользователя – DaxHR

+0

Конечно, вы инициализируете ввод: «_double input = 0; _», но вы никогда не изменяете это значение нигде в своем коде, поэтому результат умножения всегда будет 0. – Berger

+0

Хмм, если двойной вход = 0, тогда результат всегда равен 0, если двойной вход = 100, умножение работает, но я хочу, чтобы пользователь вводил число, а затем это число, которое нужно умножить – DaxHR

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

    } 
}