单击“计算”按钮时,应用程序崩溃。
package thumbplay.tip;
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;
import android.widget.Toast;
public class tip extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tip);
final EditText billamt = (EditText) findViewById(R.id.tbill);
final EditText tip_percent = (EditText) findViewById(R.id.tpercent);
final TextView totalbill = (TextView) findViewById(R.id.gt);
Button calculate = (Button) findViewById(R.id.button);
calculate.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "You have netered this ", Toast.LENGTH_SHORT).show();
final double billamt1;
final double tip_percent1;
final double totalbill1;
final double tip_cal;
billamt1 = Double.parseDouble(billamt.toString());
tip_percent1 = Double.parseDouble(tip_percent.toString());
tip_cal= (billamt1 * tip_percent1)/100; //When Calculate button clicked the app crashes
totalbill.setText(Double.toString(tip_cal));
}
}
);
}
}
单击“计算”按钮时,应用程序崩溃
答案 0 :(得分:2)
您需要先从EditText
获取文字。
billamt1=Double.parseDouble(billamt.gettext().toString());
tip_percent1=Double.parseDouble(tip_percent.gettext().toString());
由于空指针异常导致应用程序崩溃,因为您没有从EditText
获取任何数据
答案 1 :(得分:1)
错误就在这一行
billamt1 = Double.parseDouble(billamt.toString());
此处billamt
是EditText
,如果您想要它的价值,则应使用billamt.getText().toString()
同样的事情
tip_percent1 = Double.parseDouble(tip_percent.toString());
tip_percent
也是EditText
所以请使用tip_percent.getText().toString()