案列错误,程序停止运行
复选框选一个其他两个都会为true,点击寻找菜品程序会停止运行
package com.example.food; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; 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.RadioGroup.OnCheckedChangeListener; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.Toast; import android.widget.ToggleButton; public class MainActivity extends Activity { private EditText name; private RadioGroup sex; private CheckBox hot, fish, sour; private SeekBar seekBar; private Button find; private ToggleButton toggleButton; private List<Food> lists_food; private List<Food> lists_get; private Person person; private RadioGroupListener radioGroupListener; private boolean isHot, isFish, isSour; private CheckBoxListener checkBoxListener; private int price = 30; private SeekBarListener seekBarListener; private ButtonListener buttonListener; private ImageView iv_pic; private int count = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_food); intiView(); intiDate(); setListener(); } private void setListener() { seekBarListener = new SeekBarListener(); checkBoxListener = new CheckBoxListener(); radioGroupListener = new RadioGroupListener(); buttonListener = new ButtonListener(); sex.setOnCheckedChangeListener(radioGroupListener); fish.setOnCheckedChangeListener(checkBoxListener); hot.setOnCheckedChangeListener(checkBoxListener); sour.setOnCheckedChangeListener(checkBoxListener); seekBar.setOnSeekBarChangeListener(seekBarListener); find.setOnClickListener(buttonListener); toggleButton.setOnCheckedChangeListener(checkBoxListener); } private void intiDate() { person = new Person(); lists_food = new ArrayList<Food>(); lists_get = new ArrayList<Food>(); lists_food.add(new Food("麻辣香锅", 55, R.drawable.malaxiangguo, true, false, false)); lists_food.add(new Food("水煮鱼", 48, R.drawable.shuizhuyu, true, true, false)); lists_food.add(new Food("麻辣火锅", 80, R.drawable.malahuoguo, true, true, false)); lists_food.add(new Food("清蒸鲈鱼", 68, R.drawable.qingzhengluyu, false, true, false)); lists_food.add(new Food("桂林米粉", 15, R.drawable.guilin, false, false, false)); lists_food.add(new Food("上汤娃娃菜", 28, R.drawable.wawacai, false, false, false)); lists_food.add(new Food("红烧肉", 60, R.drawable.hongshaorou, false, false, false)); lists_food.add(new Food("木须肉", 40, R.drawable.muxurou, false, false, false)); lists_food.add(new Food("酸菜牛肉面", 35, R.drawable.suncainiuroumian, false, false, true)); lists_food.add(new Food("西芹炒百合", 38, R.drawable.xiqin, false, false, false)); lists_food.add(new Food("酸辣汤", 40, R.drawable.suanlatang, true, false, true)); } private void intiView() { name = (EditText) findViewById(R.id.et_name); sex = (RadioGroup) findViewById(R.id.rg_sex); hot = (CheckBox) findViewById(R.id.cb_hot); fish = (CheckBox) findViewById(R.id.cb_fish); sour = (CheckBox) findViewById(R.id.cb_sour); seekBar = (SeekBar) findViewById(R.id.seekBar); seekBar.setProgress(30); find = (Button) findViewById(R.id.btn_find); toggleButton = (ToggleButton) findViewById(R.id.toggleButton); } class RadioGroupListener implements OnCheckedChangeListener { @Override public void onCheckedChanged(RadioGroup arg0, int arg1) { // 当用户选择该RadioGroup中的Button被触发 switch (arg1) { case R.id.rb_man: person.setSex("男"); break; case R.id.rb_woman: person.setSex("女"); break; } } } class CheckBoxListener implements android.widget.CompoundButton.OnCheckedChangeListener { @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { // 复选框被点击时调用 CheckBox cbBox = (CheckBox) arg0; switch (cbBox.getId()) { case R.id.cb_fish: if (arg1) { isFish = true; } else { isFish = false; } case R.id.cb_hot: if (arg1) { isHot = true; } else { isHot = false; } case R.id.cb_sour: if (arg1) { isSour = true; } else { isSour = false; } } System.out.println("当前爱好:"+"辣"+isHot+"海鲜"+isFish+"酸"+isSour); } } class ButtonListener implements OnClickListener { @Override public void onClick(View arg0) { switch (arg0.getId()) { case R.id.btn_find: // 当用户点击寻找时,将数据进行筛查,将内容显示在ImageView里 lists_get.clear(); checkDete(); if (lists_get.size() >= 1) { showPic(count); } else { Toast.makeText(MainActivity.this, "没有搜索到内容", Toast.LENGTH_SHORT).show(); } break; case R.id.toggleButton: if (toggleButton.isChecked()) { if (lists_get.size() > 1) { count++; showPic(count); System.out.println(count); } if (count == (lists_get.size() - 1)) { count = 0; Toast.makeText(MainActivity.this, "图片显示完了,重新显示", 100) .show(); } else { Toast.makeText(MainActivity.this, "没有下一个", 100).show(); } } else { if (lists_get.size() > 0) { person.setName(name.getText().toString()); person.setFood(lists_get.get(count)); Toast.makeText(MainActivity.this, person.toString(), 1000).show(); } else { Toast.makeText(MainActivity.this, "没有此类菜品信息", 100) .show(); } } break; } } private void checkDete() { for (int i = 0; i < lists_food.size(); i++) { Food food = lists_food.get(i); if ((food.getPrice() <= price) && (food.isFish() == isFish) && (food.isHot() == isHot) && (food.isSour() == isSour)) { lists_get.add(food); } } } private void showPic(int count) { iv_pic.setImageResource(lists_get.get(count).getPic()); } } class SeekBarListener implements OnSeekBarChangeListener { @Override public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) { } @Override public void onStartTrackingTouch(SeekBar arg0) { // TODO Auto-generated method stub } @Override public void onStopTrackingTouch(SeekBar arg0) { price = arg0.getProgress(); Toast.makeText(MainActivity.this, "价格" + price, Toast.LENGTH_SHORT) .show(); } } }
80
收起
正在回答
2回答
1)CheckBoxListener中的case缺少break;2)程序停止运行的log你没有复制出来。自己根据log调一下,或上交作业,老师运行后再批注。祝:学习愉快
相似问题
登录后可查看更多问答,登录/注册
Android零基础入门2018版
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7238 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星