看板 AndroidDev 關於我們 聯絡資訊
如題 因為剛入門 到手機執行都會錯誤 可是卻可以執行 先附上程式碼 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://www.ptt.cc/bbs/AndroidDev/M.1404725770.A.307.html
corrupt003:你至少也給個log吧,程式碼全貼出來還要板友幫你build 07/07 17:56
corrupt003: 一次嗎 07/07 17:56
ms0561188:07-07 17:48:19.476: E/AndroidRuntime(26720): at 07/07 18:03
ms0561188:android.app.ActivityThread.performLaunchActivity(Act 07/07 18:03
passli:身高體重 TextView跟Edittext id都一樣 可能是這問題吧 07/07 18:03
ms0561188:ivityThread.java:1995) 07/07 18:04
ms0561188:不好意思 小弟第一次在這裡發文 有些地方還不懂 SORRY 07/07 18:04
corrupt003:passli正解,你每個view id 都改成不一樣再執行一次 07/07 18:15
corrupt003:建議class name 第一個字要大寫 07/07 18:16
ms0561188:謝謝!! 等等試試看 真不好意思麻煩了 07/07 18:17
ms0561188:我執行了結果還是一樣 07/07 18:28
ms0561188:附上剛剛的LOG圖 麻煩大大幫忙看一下 謝謝 07/07 18:34
passli:where is log ? 07/07 18:51
ms0561188:http://ppt.cc/jAhZ 07/07 19:12
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
passli:androidmanifest 有定義activity嗎? 07/07 22:39
corrupt003:你Androidmanifest定義activity的名稱是MainActivity 07/07 23:07
corrupt003:還是oaa?這兩個要一致才行 07/07 23:07
ms0561188:謝謝大大 已解決!! 07/08 11:59
ms0561188:是檔案名稱問題 謝謝!! 07/08 11:59