我制作了一个折扣应用程序,带有2个输入和一个按钮来计算结果。
此折扣应用实现了一个片段来实现tabview滑动
然而,当2 edittext或1 edittext为空时按下按钮时应用程序崩溃。
当editext为空但我没有工作时,我曾使用toast来显示。
package com.ywapp.yw.discountcal;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* A simple {@link Fragment} subclass.
*/
public class Tab1 extends Fragment {
// Variable Declaration
EditText price_box_field;
EditText discount_box_field;
TextView result_text_field;
Button cal_btn;
double price,discount,result,save;
public Tab1() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_tab1, container, false);
final EditText price_box_field=(EditText)view.findViewById(R.id.price_box);
final EditText discount_box_field=(EditText)view.findViewById(R.id.discount_price_box);
final TextView result_text_field=(TextView)view.findViewById(R.id.result_text);
Button cal_btn=(Button)view.findViewById(R.id.cal_btn);
cal_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
price=Double.parseDouble(price_box_field.getText().toString());
discount=Double.parseDouble(discount_box_field.getText().toString());
if(price_box_field.getText().toString().isEmpty()) {
Toast.makeText(getActivity(),"please enter something in text box",Toast.LENGTH_LONG).show();
}
if(discount_box_field.getText().toString().isEmpty()) {
Toast.makeText(getActivity(),"please enter something in text box",Toast.LENGTH_LONG).show();
}
result=(price-((price*discount)/100));
save=price-result;
String result_word="After Discount: RM ";
String result_set=result_word+String.format("%.2f", result)+"\nYou saved RM "+String.format("%.2f", save);
result_text_field.setText(result_set);
}
});
return view;
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
答案 0 :(得分:1)
您必须在解析值之前添加检查:
cal_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(price_box_field.getText().length()==0) {
Toast.makeText(getActivity(),"please enter something in text box",Toast.LENGTH_LONG).show();
return; //to stop the method from parsing empty values
}
if(discount_box_field.getText().length()==0) {
Toast.makeText(getActivity(),"please enter something in text box",Toast.LENGTH_LONG).show();
return; //to stop the method from parsing empty values
}
price=Double.parseDouble(price_box_field.getText().toString());
discount=Double.parseDouble(discount_box_field.getText().toString());
result=(price-((price*discount)/100));
save=price-result;
String result_word="After Discount: RM ";
String result_set=result_word+String.format("%.2f", result)+"\nYou saved RM "+String.format("%.2f", save);
result_text_field.setText(result_set);
}
});