案列错误,程序停止运行
复选框选一个其他两个都会为true,点击寻找菜品程序会停止运行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | 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 份
- 解答问题 7235 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧