2013-11-01 38 views
0

我目前正在嘗試編寫一個應用程序來計算一個人(男性/女性)所需的BMI和卡路里。 我的應用有2個部分組成: 需要輸出無法顯示在UI

1. BMI計算 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

ya..it在分配給它的textview位置處什麼也沒有顯示。 – Onered

回答

0

caloriesresult.setText(caloriesneed);

這是問題所在。在Android中,如果將純int變量分配給某個TextWidget,則會拋出錯誤,因爲Android 將其解釋爲存儲的字符串的資源ID爲,其格式爲strings.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); 

注:這是完全正常的行爲,因爲如果你正在開發羅科梅應用程序,通常你想有更多的語言支持 - 此功能需要將本地化的字符串存儲在適當的XML文件中。

+0

感謝您的詳細解釋。我只是嘗試了第一種方法,但仍然沒有顯示出來。我覺得還有其他東西丟失.. – Onered

+0

哦,我剛剛意識到這是另一個問題的單選按鈕使用。我應該使用if(male.isChecked)而不是(male.isSelected)。 – Onered

0

我想這是因爲你試圖輸入settext()一個整數,需要輸入參數string,所以你可以嘗試:

caloriesresult.setText("" + caloriesneed); 

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

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

哦...現在我明白了。 :)感謝您的快速回答。 – Onered