简体   繁体   English

当我点击保存按钮时没有输入任何内容时应用程序崩溃。 应该弹出错误

[英]App crashes when nothing is inputted when I click save button. Error should pop up

Im developing a calorie app.我正在开发一个卡路里应用程序。 The problem I'm having right now is if the user decided to press save without entering anything the app should notify the user they must input something in order to save calorie.我现在遇到的问题是,如果用户决定在不输入任何内容的情况下按保存,应用程序应通知用户他们必须输入一些内容以节省卡路里。 The app works when the user does input the proper values of the edit text.当用户输入编辑文本的正确值时,该应用程序工作。

Problem : App crashes when user clicks save button when they havent inputted anything.问题:当用户在未输入任何内容时单击保存按钮时,应用程序崩溃。

logcat.日志猫。

                                                         05-01 12:46:50.294 1810-1810/com.example.treycoco.calorietracker E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: com.example.treycoco.calorietracker, PID: 1810
                                                                               java.lang.NumberFormatException: Invalid int: ""
                                                                                   at java.lang.Integer.invalidInt(Integer.java:137)
                                                                                   at java.lang.Integer.parseInt(Integer.java:358)
                                                                                   at java.lang.Integer.parseInt(Integer.java:331)
                                                                                   at com.example.treycoco.calorietracker.AddEntry.saveDataToDB(AddEntry.java:100)
                                                                                   at com.example.treycoco.calorietracker.AddEntry$1.onClick(AddEntry.java:79)
                                                                                   at android.view.View.performClick(View.java:4438)
                                                                                   at android.view.View$PerformClick.run(View.java:18422)
                                                                                   at android.os.Handler.handleCallback(Handler.java:733)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                   at android.os.Looper.loop(Looper.java:136)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:5045)
                                                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                   at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                                                   at dalvik.system.NativeStart.main(Native Method)

AddEntry.java添加条目.java

                   public class AddEntry extends Fragment implements  
                   View.OnClickListener  {

                   EditText DescriptionET,CalorieET;
                   ImageButton Savebtn, Cancelbtn;

                   String description , calorieAmt;

                   //database
                   private DatabaseHandler dba;



                     public AddEntry() {
                   // Required empty public constructor
                   }


                     @Override
                   public View onCreateView(LayoutInflater inflater, 
                   ViewGroup container,
                         Bundle savedInstanceState) {

                     // Inflate the layout for this fragment
                   View myView = 
                  inflater.inflate(R.layout.fragment_add_entry, 
                container, false);
                 Savebtn = (ImageButton) myView.findViewById(R.id.SaveBtn);
               Savebtn.setBackgroundColor(Color.TRANSPARENT);
                Savebtn.setOnClickListener(this);


             Cancelbtn = (ImageButton) myView.findViewById(R.id.CancelBtn);
             Cancelbtn.setBackgroundColor(Color.TRANSPARENT);
                Cancelbtn.setOnClickListener(this);
                return myView;
                   }


                       @Override
            public void onViewCreated(View view, Bundle savedInstanceState)
           {
            super.onViewCreated(view, savedInstanceState);

             DescriptionET= (EditText)view.findViewById(R.id.foodEditText);

             DescriptionET.setInputType(InputType.TYPE_CLASS_TEXT);

            CalorieET=(EditText)view.findViewById(R.id.caloriesEditText);
            CalorieET.setInputType(InputType.TYPE_CLASS_NUMBER);


           //save to database:
           dba = new DatabaseHandler(getContext());


           Savebtn.setOnClickListener(new View.OnClickListener() {
             @Override
           public void onClick(View arg0) {

              saveDataToDB();


             }
         });


         }





           public void saveDataToDB (){

           Food food = new Food();
           String name = DescriptionET.getText().toString().trim();
            String calString = CalorieET.getText().toString().trim();

            //convert the claories numbers to text

           int cal = Integer.parseInt(calString);


           if (DescriptionET.getText().toString().equals("") || 
           CalorieET.getText().toString().equals("0")){

           Toast.makeText(getActivity(), "Please enter information", 
              Toast.LENGTH_LONG).show();
          }else{

        food.setFoodName(name);
        food.setCalories(cal);

        //call addFood method from the DatabaseHandler
        dba.addFood(food);
        dba.close();


        //clear the editTexts
        DescriptionET.setText("");
        CalorieET.setText("");

        //take the user to the next screen
        //


        ((appMain) getActivity()).loadSelection(0);;
    }

}


             @Override
            public void onClick(View v) {
             switch (v.getId()) {


                case R.id.SaveBtn:


            description = DescriptionET.getText().toString();
            calorieAmt=CalorieET.getText().toString();

            if (DescriptionET.getText().toString().equals("") ||
            CalorieET.getText().toString().equals("0")){

           Toast.makeText(getActivity(), "Please enter information", 
            Toast.LENGTH_LONG).show();
            }



            break;


                   case R.id.CancelBtn:

                    EditText descriptionET=  
              (EditText)getView().findViewById(R.id.foodEditText);
                 descriptionET.setText("");


            EditText calorieET= 
          (EditText)getView().findViewById(R.id.caloriesEditText);
            calorieET.setText("");

            break;
    }
}

} }

Just add a NULL check for the calories value.只需为卡路里值添加一个 NULL 检查。

if(CalorieET.getText().toString().equals("")
{
Toast.makeText(this,"Please Enter a Value", 3000)show();
}
else
{
//code here to add it to db
}

change your line改变你的线路

if (DescriptionET.getText().toString().equals("") || 
       cal==0){

       Toast.makeText(getActivity(), "Please enter information", 
          Toast.LENGTH_LONG).show();
      }
if (TextUtils.isEmpty(DescriptionET.getText().toString()) || TextUtils.isEmpty(CalorieET.getText().toString())
 {
 Toast.makeText(getActivity(), "Please enter information", 
      Toast.LENGTH_LONG).show();
 }

Actually you set onClickListener of your Savebtn to something else than the function you try to check the sanity of the input, so you need to move your condition checking before you call saveDataToDB .其实你设置onClickListener您的Savebtn的东西比你尝试检查输入的理智的功能一样,所以你需要打电话之前将您的病情检查saveDataToDB

Change this part of your code:更改这部分代码:

 Savebtn.setOnClickListener(new View.OnClickListener() {
         @Override
       public void onClick(View arg0) {
           if (DescriptionET.getText().toString().equals("") || 
               CalorieET.getText().toString().equals("0") ||
               CalorieET.getText().toString().equals("")){

                Toast.makeText(getActivity(), "Please enter information", 
                Toast.LENGTH_LONG).show();
            }
           else {
               saveDataToDB();
           }


         }
     });
          if (!calString.equals("")) {

        int cal = Integer.parseInt(calString);


            //code for database
        ;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM