我试图为我的一个朋友制作一个简单的计数器应用程序,其中一个按钮的 onClick 总数逐渐增加 1。单击按钮时我会强制关闭并且应该运行数学逻辑。LogCat 中错误的原因对我来说并不明显。这是我第一次在 Java/Android 中尝试数学(是的,我是新手)我不完全确定我是否正确处理或声明了这些值,或者是否可以这样做。看过很多例子,但它们似乎都比我真正想要的更复杂,又好又简单。作为次要问题(来自我的研究)和我自己的验证,double 是可以有小数的值,而 int 是整数?只要它们都是整数(即双精度不包含小数),它们是否可以相互替换(使用双精度而不是整数,反之亦然)?
编码
public class CounterActivity extends Activity
{
TextView tt;
Button calculate;
double x=1; //Known value to be increased by
double z=0; //The total to increase incrementally
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tt=(TextView)findViewById(R.id.tt);
calculate=(Button)findViewById(R.id.calculate);
calculate.setOnClickListener(OnClickListener);
}
Button.OnClickListener OnClickListener = new Button.OnClickListener() {
public void onClick (View calculate) {
x=Double.parseDouble("x"); //Dont believe these are set properly
z=Double.parseDouble("z");
z=x+z; //The basic loop to run onClick
tt.setText(Double.toString(z));
}
};
}
日志猫
12-16 15:58:52.194: W/dalvikvm(382): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
12-16 15:58:52.194: E/AndroidRuntime(382): Uncaught handler: thread main exiting due to uncaught exception
12-16 15:58:52.214: E/AndroidRuntime(382): java.lang.NumberFormatException:
12-16 15:58:52.214: E/AndroidRuntime(382): at org.apache.harmony.luni.util.FloatingPointParser.parseDblImpl(Native Method)
12-16 15:58:52.214: E/AndroidRuntime(382): at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:283)
12-16 15:58:52.214: E/AndroidRuntime(382): at java.lang.Double.parseDouble(Double.java:285)
12-16 15:58:52.214: E/AndroidRuntime(382): at jtek.industries.com.CounterActivity.calculate(CounterActivity.java:37)
12-16 15:58:52.214: E/AndroidRuntime(382): at jtek.industries.com.CounterActivity.access$0(CounterActivity.java:35)
12-16 15:58:52.214: E/AndroidRuntime(382): at jtek.industries.com.CounterActivity$1.onClick(CounterActivity.java:33)
12-16 15:58:52.214: E/AndroidRuntime(382): at android.view.View.performClick(View.java:2364)
12-16 15:58:52.214: E/AndroidRuntime(382): at android.view.View.onTouchEvent(View.java:4179)
12-16 15:58:52.214: E/AndroidRuntime(382): at android.widget.TextView.onTouchEvent(TextView.java:6541)
12-16 15:58:52.214: E/AndroidRuntime(382): at android.view.View.dispatchTouchEvent(View.java:3709)
一点方向将不胜感激,在我深入了解更复杂的内容之前,必须先了解基础知识:)