点击按钮闪退
来源:3-2 项目作业
weixin_慕斯4329049
2021-05-16 12:07:36
JAVA文件
public class MainActivity extends AppCompatActivity {
private EditText readerName;
private RadioGroup readerSex;
private EditText borrowTime;
private TextView returnTime;
private CheckBox historyHobby;
private CheckBox suspenseHobby;
private CheckBox litAndArtHobby;
private SeekBar ageSeekBar;
private ImageView bookImage;
private Button searchBook;
private Button next;
private ArrayList<Book> mBooks;
private Person mperson;
private ArrayList<Book> booksResult;
private Date borrowDate;
private SimpleDateFormat sdf;
private Date returnDate;
private boolean isLitAndArt;
private boolean isSuspense;
private boolean isHistory;
private TextView starAge;
private int bookIndex;
private Book book;
private int readerAge;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//初始化控件
findView();
//初始化数据
dataInit();
//添加监听器
addListener();
}
private void addListener() {
//性别监听器
readerSex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.mealRadioButton:
mperson.setSex("男");
break;
case R.id.femealRadioButton:
mperson.setSex("女");
break;
}
}
});
//设置日期文本监听器
borrowTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf = new SimpleDateFormat("yyyy-MM-dd");
try {
//String转换为Date
borrowDate = sdf.parse(borrowTime.getText().toString());
returnDate = sdf.parse(returnTime.getText().toString());
} catch (ParseException e) {
e.printStackTrace();
}
}
});
//爱好监听器
historyHobby.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
isHistory = isChecked;
}
});
suspenseHobby.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
isSuspense = isChecked;
}
});
litAndArtHobby.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
isLitAndArt = isChecked;
}
});
//设置年龄监听器
ageSeekBar.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) {
starAge = findViewById(R.id.starAgeTextView);
starAge.setText(ageSeekBar.getProgress()+"");
}
});
//查找按钮监听器
searchBook.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//判断时间
if (borrowDate.after(returnDate)){
Toast.makeText(MainActivity.this,"借书年份晚于还书年份,退出",Toast.LENGTH_SHORT);
// finish();
}
search();
}
});
}
private void search() {
if (booksResult == null){
booksResult = new ArrayList<>();
}
booksResult.clear();
//第几本书
bookIndex = 0;
//遍历书
for (int i = 0; i < mBooks.size(); i++) {
book = mBooks.get(i);
if (book != null){
if (book.getSuitableAge()<readerAge&&
(book.isHistory() == isHistory ||
book.isSuspense() == isSuspense ||
book.isLitAndArt() == isLitAndArt)){
booksResult.add(book);
}
}
}
if (bookIndex<mBooks.size()){
bookImage.setImageResource(booksResult.get(bookIndex).getPicture());
}
}
private void dataInit() {
mBooks = new ArrayList<>();
mBooks.add(new Book("人生感悟",30,"哲理",R.drawable.aa,false,false,true));
mBooks.add(new Book("边城",20,"回忆录",R.drawable.bb,true,false,true));
mBooks.add(new Book("saplr",18,"技术",R.drawable.cc,false,false,true));
mBooks.add(new Book("光辉岁月",40,"回忆录",R.drawable.dd,true,false,false));
mBooks.add(new Book("唐诗三百首",18,"诗词",R.drawable.ee,true,false,true));
mBooks.add(new Book("艺术画作",15,"艺术",R.drawable.f,false,false,true));
mBooks.add(new Book("中国古代文学纲要",18,"教学",R.drawable.ff,true,false,false));
mBooks.add(new Book("无花果",25,"叙述",R.drawable.gg,false,false,true));
mBooks.add(new Book("古镇记忆",18,"回忆录",R.drawable.hh,false,false,true));
mperson = new Person();
booksResult = new ArrayList<>();
}
private void findView() {
readerName = findViewById(R.id.nameEditText);
readerSex = findViewById(R.id.sexRadioGroup);
borrowTime = findViewById(R.id.borrowTimeEditText);
returnTime = findViewById(R.id.returnTimeTextView);
historyHobby = findViewById(R.id.historyCheckBox);
suspenseHobby = findViewById(R.id.suspenseCheckBox);
litAndArtHobby = findViewById(R.id.LitAndArtCheckBox);
ageSeekBar = findViewById(R.id.ageSeekBar);
bookImage = findViewById(R.id.image);
searchBook = findViewById(R.id.searchButton);
next = findViewById(R.id.nextButton);
}
}
XML文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="#FAFADA">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="借书"
android:textSize="30sp"
android:textStyle="bold"
android:layout_marginLeft="20dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="vertical">
<!-- 读者 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="读者:"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="10dp"/>
<EditText
android:id="@+id/nameEditText"
android:layout_width="250dp"
android:layout_height="match_parent"
android:hint="输入姓名" />
</LinearLayout>
<!-- 性别 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="性别:"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="10dp"/>
<RadioGroup
android:id="@+id/sexRadioGroup"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<RadioButton
android:id="@+id/mealRadioButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="男"
android:textSize="20sp"
android:layout_marginRight="10dp"/>
<RadioButton
android:id="@+id/femealRadioButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="女"
android:textSize="20sp"
android:layout_marginRight="10dp"/>
</RadioGroup>
</LinearLayout>
<!-- 借出时间 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="借出时间:"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="10dp"/>
<EditText
android:id="@+id/borrowTimeEditText"
android:layout_width="250dp"
android:layout_height="match_parent"
android:hint="输入格式为YYYY-MM-DD"/>
</LinearLayout>
<!-- 归还时间 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="归还时间:"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="10dp"/>
<TextView
android:id="@+id/returnTimeTextView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="2016-06-30"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<!-- 爱好 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="爱好:"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="10dp"/>
<CheckBox
android:id="@+id/historyCheckBox"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="历史"
android:textSize="20sp"
android:layout_marginRight="10dp"/>
<CheckBox
android:id="@+id/suspenseCheckBox"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="悬疑"
android:textSize="20sp"
android:layout_marginRight="10dp"/>
<CheckBox
android:id="@+id/LitAndArtCheckBox"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="文艺"
android:textSize="20sp"
android:layout_marginRight="10dp"/>
</LinearLayout>
<!-- 年龄 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="年龄:"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="10dp"/>
<TextView
android:id="@+id/starAgeTextView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="18"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="10dp"/>
<SeekBar
android:id="@+id/ageSeekBar"
android:layout_width="150dp"
android:layout_height="match_parent"
android:progress="18"
android:min="0"
android:max="100"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="100"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="10dp"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:orientation="horizontal"
android:background="#FFFFFF">
<ImageView
android:id="@+id/image"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/bookeNameTextView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="书名"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="10dp"/>
<TextView
android:id="@+id/typeTextView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="类型"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="10dp"/>
<TextView
android:id="@+id/suitableAgeTextView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="适用年龄"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:layout_marginLeft="10dp"/>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<Button
android:id="@+id/searchButton"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="21dp"
android:background="#0000ff"
android:text="查找" />
<Button
android:id="@+id/nextButton"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="20dp"
android:background="#0000ff"
android:text="下一个" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
麻烦解答一下,为啥输入信息(输入的格式正确)之后,点击查找按钮会闪退?
下面这个是闪退后的Logcat,但是看了一下,还是没看出来
1回答
LovelyChubby
2021-05-17
按钮点击的时候,borrowDate 对象还没被创建,没有赋值,就会报错了。
你可以在这里判断一下,如果borrowDate为空则return
相似问题