2013-11-01 4 views
0

현재 사람 (남성/여성)이 필요로하는 BMI 및 칼로리를 계산하는 앱을 작성하려고합니다. (그래서이 부분에 대한 코드를 제외) 첫 번째 부분은 잘 작동UI에서 출력을 표시 할 수 없습니다.

필요 1. BMI 계산 2. 칼로리,하지만 2의 경우, 칼로리를 필요로 계산, 그것은되지 않습니다 : 내 애플 리케이션이 개 부분이 예상대로 결과를 표시 할 수 있습니다 ('칼로리 필요'버튼을 클릭 한 후). 아마도 뭔가가 여전히 누락되어 있지만 지금까지 찾을 수 없습니다.

코드에서 문제가 없으며 오류가 없습니다. 아무도 살펴 볼 수 있습니까? :) 미리 감사드립니다. 아래

코드 :

package com.example.caloriescalculator; 

import android.app.Activity; 
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; 
import android.widget.RadioButton; 

public class BMIcalculation extends Activity 
{ 
EditText weightE; 
EditText heightE ; 
EditText ageE ; 
TextView caloriesresult ; 
RadioButton male; 
RadioButton female; 

Button calories; 

EditText weightText ; 
EditText heightText ; 
EditText ageText; 
TextView resultText; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.bmilayout_main);  

    weightE = (EditText)findViewById(R.id.weightText); 
    heightE = (EditText)findViewById(R.id.heightText); 
    ageE = (EditText)findViewById(R.id.ageText); 
    caloriesresult = (TextView)findViewById(R.id.caloriesText); 
    male = (RadioButton) findViewById(R.id.maleradio); 
    female = (RadioButton) findViewById(R.id.femaleradio); 

    Button calories = (Button) findViewById(R.id.caloriesButton); 
    calories.setOnClickListener(new OnClickListener() 
     { 
     @Override 
       public void onClick(View arg0) 
       {  
        int caloriesneed = 0, weight = 0, height = 0, age = 0; 

        try { 
          if ((!weightE.equals("")) && (!heightE.equals("")) && (!ageE.equals(""))) 
           { 
           weightE = (EditText) findViewById(R.id.weightText); 
           weight = Integer.parseInt(weightE.getText().toString().trim());  
           heightE = (EditText) findViewById(R.id.heightText); 
           height = Integer.parseInt(heightE.getText().toString().trim()); 
           ageE = (EditText) findViewById(R.id.ageText); 
           age = Integer.parseInt(ageE.getText().toString().trim()); 

            if (male.isSelected()) 
             { 
              caloriesneed = (int) Math.round (655 + 9.6*weight + 1.8*height - 4.7*age);          
              caloriesresult.setText(caloriesneed); 
             } 

            else if (female.isSelected()) 
             { 
              caloriesneed = (int) Math.round (66 + 13.7*weight + 5*height - 6.8*age); 
              caloriesresult.setText(caloriesneed); 
             } 
           } 
         } 
        catch (Exception k) 
        { System.out.println(k); 

        } 

       } 
     }); 
} 
+0

코드를 기록하거나 오류 지점을 디버그하려면 디버그 지점을 기록하십시오. –

+0

"예상대로 결과를 표시 할 수 없습니다."이게 무슨 뜻입니까? 잘못된 출력을 보이고 있거나 아무것도 보이지 않는 것입니까? –

+0

나중에 .. 그것에 할당 된 textview 장소에서 아무것도 보이지 않습니다. – Onered

답변

0

caloriesresult.setText (caloriesneed);

이것은 문제입니다. 안드로이드에서 어떤 TextWidget에 순수 int 변수를 할당하면, 은 저장된 문자열의 resouce idstrings.xml으로 해석하기 때문에 오류가 발생합니다.

따라서 명시 적 전송이 필요합니다. 당신은 concentation 또는 String.valueOf(int value) 또는 Integer.toString(int value) 방법의 사용과 그것을 달성 할 수

textWidget.setText(Integer.toString(value)); // best approach 
textWidget.setText(String.valueOf(value)); 
textWidget.setText("" + value); 

참고 : 이것은 당신이 COMERCIAL 응용 프로그램을 개발하는 경우, 일반적으로 더 많은 언어에 대한 지원을 갖고 싶어하기 때문에 절대적으로 정상 동작합니다 - 이 기능을 사용하려면 현지화 된 문자열이 적절한 XML 파일에 저장되어 있어야합니다.

+0

자세한 설명을 해주셔서 감사합니다. 방금 첫 번째 접근 방식으로 시도했지만 여전히 아무것도 표시되지 않습니다. 나는 뭔가 다른 것이 누락 된 것 같아 .. – Onered

+0

오, 방금 라디오 버튼 사용에 또 다른 문제가 있음을 깨달았습니다. 대신 (male.isSelected) 대신 if (male.isChecked)를 사용해야합니다. – Onered

0

난 당신이 입력 매개 변수에 대한 string 필요 settext()의 정수를 입력하려고하기 때문에 당신이 시도 할 수 있도록이 같아요 :

caloriesresult.setText("" + caloriesneed); 

또는

caloriesresult.setText(Integer.toString(caloriesneed)); 

또는

caloriesresult.setText(String.valueOf(caloriesneed)); 
+0

오우 ... 이제 이해합니다. :) 빠른 답변 주셔서 감사합니다. – Onered

관련 문제