我是新手程序员,现在已经在大学课程中使用Eclipse大约3个月了。我正在尝试制作一个简化的计算器,用于计算菜单价格成本并返回菜单价格和成本百分比的理想值。该计划表示没有错误,但如果我通过编程学到了什么,那么这并不意味着蹲下来。当应用程序运行时,我可以输入数字,我的"清除"按钮工作正常,但当我点击"计算"按钮我收到一条错误消息,指出应用程序已崩溃。
下面是我的mainactivity.java文件:
package edu.niagara.cis.coveringcostsbeta;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText m1, m2, m3, m4;
TextView sum1, sum2, sum3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m1 = (EditText) findViewById(R.id.price);
m2 = (EditText) findViewById(R.id.recipe);
m3 = (EditText) findViewById(R.id.portion);
m4 = (EditText) findViewById(R.id.costBudget);
sum1 = (TextView) findViewById(R.id.costPortion);
sum2 = (TextView) findViewById(R.id.idealPrice);
sum3 = (TextView) findViewById(R.id.costActual);
}
public void goCalculate(View v) {
String str_m1 = m1.getText().toString();
String str_m2 = m2.getText().toString();
String str_m3 = m3.getText().toString();
String str_m4 = m4.getText().toString();
if (str_m1.equals(""))
str_m1 = "0";
if (str_m2.equals(""))
str_m2 = "0";
if (str_m3.equals(""))
str_m3 = "0";
if (str_m4.equals(""))
str_m4 = "0";
int i_m1 = Integer.parseInt(str_m1);
int i_m2 = Integer.parseInt(str_m2);
int i_m3 = Integer.parseInt(str_m3);
int i_m4 = Integer.parseInt(str_m4);
long l_sum1 = (long) (i_m2 / i_m3);
long l_sum2 = (long) ((i_m2 / i_m3) / i_m4);
long l_sum3 = (long) (l_sum1 / i_m1);
String str_sum1 = Long.toString(l_sum1);
sum1.setText(str_sum1);
String str_sum2 = Long.toString(l_sum2);
sum2.setText(str_sum2);
String str_sum3 = Long.toString(l_sum3);
sum3.setText(str_sum3);
}
public void goClear(View v) {
m1.setText("");
m2.setText("");
m3.setText("");
m4.setText("");
sum1.setText("Cost Per Portion");
sum2.setText("Ideal Price");
sum3.setText("Food Cost Actual");
}
}
和我的mainactivity.xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:text="My Menu Price"
android:ems="10" >
</EditText>
<EditText
android:id="@+id/recipe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:text="My Recipe Cost"
android:ems="10" >
</EditText>
<EditText
android:id="@+id/portion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:text="# of Portions"
android:ems="10">
</EditText>
<EditText
android:id="@+id/costBudget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="numberDecimal"
android:text="Food Cost Budget %"
android:ems="10">
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="goCalculate"
android:text="Calculate" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="goClear"
android:text="Clear" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/costPortion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cost Per Portion"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/idealPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ideal Price"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/costActual"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Food Cost Actual"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
</LinearLayout>
我不认为它与xml文件有关,但我想提供尽可能多的信息。我几乎肯定我的计算导致了崩溃,但我不知道具体是如何造成这种情况的。是因为在我的计算中,其中一个变量是另一个总和? :
long l_sum1 = (long) (i_m2 / i_m3);
long l_sum2 = (long) ((i_m2 / i_m3) / i_m4);
long l_sum3 = (long) (l_sum1 / i_m1);
LogCat也没有多大帮助,弹出的唯一信息是: E -1设备断开:1 我也尝试过清理程序并再次运行它,但这并没有改变任何东西。
我很乐意发布任何其他信息,如果它有助于更有意义。我非常感谢任何帮助/批评。
-Joe
这是实际的ACTUAL LogCat,抱歉混淆了!
04-07 14:39:46.971: D/dalvikvm(1704): Not late-enabling CheckJNI (already on)
04-07 14:39:50.981: D/(1704): HostConnection::get() New Host Connection established 0xb8062d20, tid 1704
04-07 14:39:51.081: W/EGL_emulation(1704): eglSurfaceAttrib not implemented
04-07 14:39:51.111: D/OpenGLRenderer(1704): Enabling debug mode 0
04-07 14:40:02.001: W/EGL_emulation(1704): eglSurfaceAttrib not implemented
04-07 14:40:03.961: D/dalvikvm(1704): GC_FOR_ALLOC freed 146K, 7% free 3091K/3308K, paused 25ms, total 32ms
04-07 14:40:10.411: W/EGL_emulation(1704): eglSurfaceAttrib not implemented
04-07 14:40:14.152: W/EGL_emulation(1704): eglSurfaceAttrib not implemented
04-07 14:40:19.062: W/EGL_emulation(1704): eglSurfaceAttrib not implemented
04-07 14:40:27.232: W/EGL_emulation(1704): eglSurfaceAttrib not implemented
04-07 14:40:29.772: W/EGL_emulation(1704): eglSurfaceAttrib not implemented
04-07 14:40:30.752: W/EGL_emulation(1704): eglSurfaceAttrib not implemented
04-07 14:40:32.762: D/dalvikvm(1704): GC_FOR_ALLOC freed 421K, 14% free 3184K/3676K, paused 27ms, total 43ms
04-07 14:40:37.252: D/AndroidRuntime(1704): Shutting down VM
04-07 14:40:37.252: W/dalvikvm(1704): threadid=1: thread exiting with uncaught exception (group=0xb2cf2b20)
04-07 14:40:37.262: E/AndroidRuntime(1704): FATAL EXCEPTION: main
04-07 14:40:37.262: E/AndroidRuntime(1704): Process: edu.niagara.cis.coveringcostsbeta, PID: 1704
04-07 14:40:37.262: E/AndroidRuntime(1704): java.lang.IllegalStateException: Could not execute method of the activity
04-07 14:40:37.262: E/AndroidRuntime(1704): at android.view.View$1.onClick(View.java:3823)
04-07 14:40:37.262: E/AndroidRuntime(1704): at android.view.View.performClick(View.java:4438)
04-07 14:40:37.262: E/AndroidRuntime(1704): at android.view.View$PerformClick.run(View.java:18422)
04-07 14:40:37.262: E/AndroidRuntime(1704): at android.os.Handler.handleCallback(Handler.java:733)
04-07 14:40:37.262: E/AndroidRuntime(1704): at android.os.Handler.dispatchMessage(Handler.java:95)
04-07 14:40:37.262: E/AndroidRuntime(1704): at android.os.Looper.loop(Looper.java:136)
04-07 14:40:37.262: E/AndroidRuntime(1704): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-07 14:40:37.262: E/AndroidRuntime(1704): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 14:40:37.262: E/AndroidRuntime(1704): at java.lang.reflect.Method.invoke(Method.java:515)
04-07 14:40:37.262: E/AndroidRuntime(1704): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-07 14:40:37.262: E/AndroidRuntime(1704): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-07 14:40:37.262: E/AndroidRuntime(1704): at dalvik.system.NativeStart.main(Native Method)
04-07 14:40:37.262: E/AndroidRuntime(1704): Caused by: java.lang.reflect.InvocationTargetException
04-07 14:40:37.262: E/AndroidRuntime(1704): at java.lang.reflect.Method.invokeNative(Native Method)
04-07 14:40:37.262: E/AndroidRuntime(1704): at java.lang.reflect.Method.invoke(Method.java:515)
04-07 14:40:37.262: E/AndroidRuntime(1704): at android.view.View$1.onClick(View.java:3818)
04-07 14:40:37.262: E/AndroidRuntime(1704): ... 11 more
04-07 14:40:37.262: E/AndroidRuntime(1704): Caused by: java.lang.NumberFormatException: Invalid int: "9.00"
04-07 14:40:37.262: E/AndroidRuntime(1704): at java.lang.Integer.invalidInt(Integer.java:137)
04-07 14:40:37.262: E/AndroidRuntime(1704): at java.lang.Integer.parse(Integer.java:374)
04-07 14:40:37.262: E/AndroidRuntime(1704): at java.lang.Integer.parseInt(Integer.java:365)
04-07 14:40:37.262: E/AndroidRuntime(1704): at java.lang.Integer.parseInt(Integer.java:331)
04-07 14:40:37.262: E/AndroidRuntime(1704): at edu.niagara.cis.coveringcostsbeta.MainActivity.goCalculate(MainActivity.java:43)
04-07 14:40:37.262: E/AndroidRuntime(1704): ... 14 more
答案 0 :(得分:0)
似乎问题从这里开始:
04-07 14:40:37.262: E/AndroidRuntime(1704): at edu.niagara.cis.coveringcostsbeta.MainActivity.goCalculate(MainActivity.java:43)
这是什么代码?
答案 1 :(得分:0)
根据这个
java.lang.NumberFormatException: Invalid int: "9.00"
你试图将浮点数转换为第43行的整数
edu.niagara.cis.coveringcostsbeta.MainActivity.goCalculate(MainActivity.java:43)
你应该设置
android:inputType="number"
到您的EditTexts。