我的代码其他功能都正常,唯独“判断借出时间”那里,一输入借出时间就停止运行,这段代码该如何更改?

我的代码其他功能都正常,唯独“判断借出时间”那里,一输入借出时间就停止运行,这段代码该如何更改?

package com.example.book;

import androidx.appcompat.app.AppCompatActivity;


import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.Toast;
import com.example.book.model.Book;
import com.example.book.model.Person;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private EditText person_name_EditText;
    private EditText borrow_time_EditText;
    private EditText back_time_EditText;
    private RadioGroup sex_radiogroup;
    private CheckBox history_checkbox,suspense_checkbox,art_checkbox;
    private SeekBar age_seekbar;
    private ImageView book_image;
    private EditText book_name_EditText;
    private EditText book_type_EditText;
    private EditText book_age_EditText;
    private Button find_button;
    private Button next_button;

    private List<Book> books;
    private List<Book> books_search;
    private StringBuilder book_type;
    private Person person;

    private Date borrow_time_date;
    private Date back_time_date;

    private boolean is_history = false;
    private boolean is_suspense = false;
    private boolean is_art = false;

    private int age;

    private int current_index;






    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化控件
        findViews();

        //初始化数据
        person = new Person();
        initData();

        // 为控件添加监听器,实现控件的基本功能
        setListeners();

    }

    private void setListeners() {

        // 监听用户输入的姓名
        person_name_EditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(person != null){
                    person.setPerson_name(s.toString());
                }
            }
        });

        // 监听用户选择的性别
        sex_radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.male_radiobutton:
                        person.setSex("男");
                        break;
                    case R.id.female_radiobutton:
                        person.setSex("女");
                        break;
                }
            }
        });

        // 监听用户输入的借出时间
        borrow_time_EditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(person != null){
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    person.setBorrow_time(s.toString());
                    /*try {
                        borrow_time_date = sdf.parse(person.getBorrow_time());
                        back_time_date = sdf.parse("2020-02-28");
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    if(borrow_time_date.after(back_time_date)){
                            Toast.makeText(MainActivity.this,"借出时间超出归还时间!",Toast.LENGTH_SHORT);
                    }*/
                }
            }
        });

        // 监听用户选择的爱好
        history_checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_history = isChecked;

            }
        });

        suspense_checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_suspense = isChecked;
            }
        });

        art_checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                is_art = isChecked;
            }
        });

        // 监听用户选择的年龄
        age_seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                age = seekBar.getProgress();
                Toast.makeText(MainActivity.this,"年龄:"+ age,Toast.LENGTH_SHORT).show();
            }
        });

        // 查找书籍
        find_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                search();
                String person_name = person_name_EditText.getText().toString();
                String sex = person.getSex();
                String borrow_time = person.getBorrow_time();
                Toast.makeText(MainActivity.this,"姓名:"+ person_name + "   年龄:"+ age + "   性别:" + sex+"\n借出时间:" + borrow_time,Toast.LENGTH_SHORT).show();

            }
        });

        //浏览下一个符合条件的书籍
        next_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               if(next_button.isClickable()){
                   current_index ++;
                   if(current_index < books_search.size()){
                       book_image.setImageResource(books_search.get(current_index).getPic());
                       book_name_EditText.setText(books_search.get(current_index).getBook_name().toString());
                       book_age_EditText.setText(books_search.get(current_index).getBook_age()+ " ");
                       book_type_EditText.setText(books_search.get(current_index).getBook_type().toString());
                   }
               }
            }
        });
    }



    private void search() {
        // 遍历所有书籍,找到符合条件的,就把它放在search_books集合中
        // 每次查找结束,都清空集合

        // 如果集合为空,先初始化
        if(books_search == null){
            books_search = new ArrayList<>();
        }

        books_search.clear();
        current_index = 0;

        for(int index = 0;index<books.size();index ++){
            Book book = books.get(index);
            if(books != null){
                if((book.getBook_age()>=age)&&
                                (book.isHistory()==is_history
                                ||book.isSuspense()==is_suspense
                                ||book.isArt()==is_art)){
                    books_search.add(book);
                }
            }
        }
        if (current_index < books_search.size()){
            book_image.setImageResource(books_search.get(current_index).getPic());
            book_name_EditText.setText(books_search.get(current_index).getBook_name().toString());
            book_age_EditText.setText(books_search.get(current_index).getBook_age()+ " ");
            book_type_EditText.setText(books_search.get(current_index).getBook_type().toString());


        }
    }

    // 初始化数据的方法
    private void initData() {
        books = new ArrayList<Book>();
        books_search = new ArrayList<>();


        books.add(new Book("感悟人生",25,true,false,true,R.drawable.aa ,"历史、文艺"));
        books.add(new Book("边城",20,true,false,true,R.drawable.bb,"历史、文艺"));
        books.add(new Book("spair",30,false,true,false,R.drawable.cc,"悬疑"));
        books.add(new Book("光辉岁月",40,true,true,false,R.drawable.dd,"历史、悬疑"));
        books.add(new Book("宋词三百首",5,true,false,true,R.drawable.ee,"历史、文艺"));
        books.add(new Book("中国古代文学",20,true,false,true,R.drawable.ff,"历史、文艺"));
        books.add(new Book("无花果",18,true,true,true,R.drawable.gg,"历史、悬疑、文艺"));
        books.add(new Book("古镇记忆",18,true,true,true,R.drawable.hh,"历史、文艺、悬疑"));

    }

    // 初始化控件的方法
    private void findViews() {
        person_name_EditText = findViewById(R.id.person_name_EditText);
        sex_radiogroup = findViewById(R.id.sex_radiogroup);
        borrow_time_EditText = findViewById(R.id.borrow_time_EditText);
        back_time_EditText = findViewById(R.id.back_time_EditText);
        history_checkbox = findViewById(R.id.history_checkbox);
        suspense_checkbox = findViewById(R.id.suspense_checkbox);
        art_checkbox = findViewById(R.id.art_checkbox);
        age_seekbar = findViewById(R.id.age_seekbar);
        age_seekbar.setProgress(18);
        book_image = findViewById(R.id.picture_image);
        book_name_EditText = findViewById(R.id.book_name_EditText);
        book_type_EditText = findViewById(R.id.book_type_EditText);
        book_age_EditText = findViewById(R.id.book_age_EditText);
        find_button = findViewById(R.id.find_button);
        next_button = findViewById(R.id.next_button);
    }
}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

4回答
Charles_hzr 2020-09-21 14:07:41

sdf.parse参数如果不是正常格式的参数,就会出错退出。

你现在放在EditText的监听里,所以你输入一个字就会跑一次parse,所以出错。

我觉得应该放在search button的监听里再处理。

提问者 菜鸟迪 2020-02-29 19:30:20
  • 同学理解错了,1)你需要把控制台的报错信息粘贴一下。 2)你在运行时,给它输入的值是多少,需要说明一下
    2020-02-29 20:20:39
好帮手慕雪 2020-02-29 19:07:27

你输入的值是什么呢?另外你需要把错误信息粘贴一下。祝:学习愉快

  • 提问者 菜鸟迪 #1
    在输入借出时间的时候,只按一个数字,就退出了
    2020-02-29 19:29:38
提问者 菜鸟迪 2020-02-29 15:08:27

复制代码太乱,这里有截图http://img1.sycdn.imooc.com//climg/5e5a0de2086b385208340867.jpg

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师