如題 因為剛入門 到手機執行都會錯誤 可是卻可以執行
先附上程式碼
package com.example.oaa;
import java.text.DecimalFormat;
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;
public class oaa extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Listen for button clicks
Button button = (Button)findViewById(R.id.submit);
button.setOnClickListener(calcBMI);
}
private OnClickListener calcBMI = new OnClickListener()
{
public void onClick(View v)
{
DecimalFormat nf = new DecimalFormat("0.00");
EditText fieldheight = (EditText)findViewById(R.id.height);
EditText fieldweight = (EditText)findViewById(R.id.weight);
double height =
Double.parseDouble(fieldheight.getText().toString())/100;
double weight =
Double.parseDouble(fieldweight.getText().toString());
double BMI = weight / (height * height);
TextView result = (TextView)findViewById(R.id.result);
result.setText("Your BMI is "+nf.format(BMI));
//Give health advice
TextView fieldsuggest = (TextView)findViewById(R.id.suggest);
if(BMI>25){
fieldsuggest.setText(R.string.advice_heavy);
}else if(BMI<20){
fieldsuggest.setText(R.string.advice_light);
}else{
fieldsuggest.setText(R.string.advice_average);
}
}
};
}
另外還有XML的檔案
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/height"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="身高 (cm)" />
<EditText
android:id="@+id/height"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:text=""
/>
<TextView
android:id="@+id/weight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="體重 (kg)" />
<EditText
android:id="@+id/weight"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:text=""
/>
<Button
android:id="@+id/submit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="計算 BMI 值"
/>
<TextView
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
<TextView
android:id="@+id/suggest"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text=""
/>
</LinearLayout>
煩請高手幫解決 謝謝!!!