onClick 侦听器在所有情况下都会导致应用程序崩溃
onClick listener crashing app in all cases
我正在尝试创建一个简单的 Android 应用程序来计算税金,但每当我点击我创建的按钮时,我都会收到消息 "unfortunately blah blah has stopped"
这是我的 Java:
public class MainActivity extends ActionBarActivity {
EditText propValue, stampDuty;
Button calculate;
Double x, y;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start();
}
private void start() {
propValue=(EditText)findViewById(R.id.propValue);
calculate=(Button)findViewById(R.id.calculate);
calculate.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
calculate();
}
});
}
private void calculate()
{
x=Double.parseDouble(propValue.getText().toString());
if (x <= 125000 ) {
y = 0.0;
} else if (x <= 250000) {
y = (x - 125000) * 0.02;
} else if (x <= 925000) {
y = (125000 * 0.02) + ((x - 250000) * 0.05);
} else if (x <= 1500000) {
y = (125000 * 0.02) + (675000 * 0.05) + ((x - 925000) * 0.1);
} else if (x > 150000) {
y = (125000 * 0.02) + (675000 * 0.05) + (575000 * 0.1) + ((x - 1500000) * 0.12);
}
stampDuty.setText(Double.toString(y));
}
}
我绝对只是将数字放入 EditText。我输入的任何带或不带小数位的数字。以下是相关的布局部分:
<EditText
android:id="@+id/propValue"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:minLines="1"
android:maxLines="1"/>
<Button
android:id="@+id/calculate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button"
android:layout_margin="10dp"/>
<TextView
android:id="@+id/stampDuty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:minLines="1"
android:maxLines="1"/>
stampDuty 从未初始化。
您将 stampDuty 声明为 EditText,但在您的 xml 中是一个 TextView。
将 stampDuty 更改为 TextView 然后在 start()
中您可以添加:
stampDuty=(TextView)findViewById(R.id.stampDuty);
我正在尝试创建一个简单的 Android 应用程序来计算税金,但每当我点击我创建的按钮时,我都会收到消息 "unfortunately blah blah has stopped"
这是我的 Java:
public class MainActivity extends ActionBarActivity {
EditText propValue, stampDuty;
Button calculate;
Double x, y;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start();
}
private void start() {
propValue=(EditText)findViewById(R.id.propValue);
calculate=(Button)findViewById(R.id.calculate);
calculate.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
calculate();
}
});
}
private void calculate()
{
x=Double.parseDouble(propValue.getText().toString());
if (x <= 125000 ) {
y = 0.0;
} else if (x <= 250000) {
y = (x - 125000) * 0.02;
} else if (x <= 925000) {
y = (125000 * 0.02) + ((x - 250000) * 0.05);
} else if (x <= 1500000) {
y = (125000 * 0.02) + (675000 * 0.05) + ((x - 925000) * 0.1);
} else if (x > 150000) {
y = (125000 * 0.02) + (675000 * 0.05) + (575000 * 0.1) + ((x - 1500000) * 0.12);
}
stampDuty.setText(Double.toString(y));
}
}
我绝对只是将数字放入 EditText。我输入的任何带或不带小数位的数字。以下是相关的布局部分:
<EditText
android:id="@+id/propValue"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:minLines="1"
android:maxLines="1"/>
<Button
android:id="@+id/calculate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button"
android:layout_margin="10dp"/>
<TextView
android:id="@+id/stampDuty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:minLines="1"
android:maxLines="1"/>
stampDuty 从未初始化。
您将 stampDuty 声明为 EditText,但在您的 xml 中是一个 TextView。
将 stampDuty 更改为 TextView 然后在 start()
中您可以添加:
stampDuty=(TextView)findViewById(R.id.stampDuty);