作者ms0561188 (说说)
看板AndroidDev
标题[问题] 刚入门BMI问题(已有附log)
时间Mon Jul 7 17:36:05 2014
如题 因为刚入门 到手机执行都会错误 可是却可以执行
先附上程式码
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>
烦请高手帮解决 谢谢!!!
--
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 210.61.163.55
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/AndroidDev/M.1404725770.A.307.html
1F:→ corrupt003:你至少也给个log吧,程式码全贴出来还要板友帮你build 07/07 17:56
2F:→ corrupt003: 一次吗 07/07 17:56
3F:→ ms0561188:07-07 17:48:19.476: E/AndroidRuntime(26720): at 07/07 18:03
4F:→ ms0561188:android.app.ActivityThread.performLaunchActivity(Act 07/07 18:03
5F:→ passli:身高体重 TextView跟Edittext id都一样 可能是这问题吧 07/07 18:03
6F:→ ms0561188:ivityThread.java:1995) 07/07 18:04
7F:→ ms0561188:不好意思 小弟第一次在这里发文 有些地方还不懂 SORRY 07/07 18:04
8F:→ corrupt003:passli正解,你每个view id 都改成不一样再执行一次 07/07 18:15
9F:→ corrupt003:建议class name 第一个字要大写 07/07 18:16
10F:→ ms0561188:谢谢!! 等等试试看 真不好意思麻烦了 07/07 18:17
11F:→ ms0561188:我执行了结果还是一样 07/07 18:28
12F:→ ms0561188:附上刚刚的LOG图 麻烦大大帮忙看一下 谢谢 07/07 18:34
13F:→ passli:where is log ? 07/07 18:51
15F:→ ms0561188:不好意思刚刚被叫去帮忙东西 07/07 19:13
※ 编辑: ms0561188 (210.61.163.55), 07/07/2014 19:16:17
※ 编辑: ms0561188 (210.61.163.55), 07/07/2014 19:21:31
16F:→ passli:androidmanifest 有定义activity吗? 07/07 22:39
17F:→ corrupt003:你Androidmanifest定义activity的名称是MainActivity 07/07 23:07
18F:→ corrupt003:还是oaa?这两个要一致才行 07/07 23:07
19F:→ ms0561188:谢谢大大 已解决!! 07/08 11:59
20F:→ ms0561188:是档案名称问题 谢谢!! 07/08 11:59