4-5作业问题查找直接提示错误?
class ButtonListener implements OnClickListener { @Override public void onClick(View arg0) { switch (arg0.getId()) { case R.id.btn_find: // 当用户点击寻找时,需要筛查信息,并把结果显示在imageView上 lists_get.clear(); count = 0; checkData(); if (lists_get.size() > 0) { showPic(count); } else { Toast.makeText(MainActivity.this, "没有相关菜品信息", 100).show(); System.out.println("没有相关菜品信息"); } break; case R.id.tb_click: break; } } private void checkData() { // 找出菜品 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); } } System.out.println("*************" + lists_get.size() + "****************"); } } private void showPic(int count) { // count值为要显示lists_get列表中第几个菜品的图片 // count值从0开始,表示显示lists_get列表中第一次菜品的图片 iv_pic.setImageResource(lists_get.get(count).getPic()); } }
08-01 03:32:44.336: D/AndroidRuntime(2682): Shutting down VM
08-01 03:32:44.340: W/dalvikvm(2682): threadid=1: thread exiting with uncaught exception (group=0xa4cd7b20)
08-01 03:32:44.364: E/AndroidRuntime(2682): FATAL EXCEPTION: main
08-01 03:32:44.364: E/AndroidRuntime(2682): Process: com.imooc.ui_basetestdemo_food, PID: 2682
08-01 03:32:44.364: E/AndroidRuntime(2682): java.lang.NullPointerException
08-01 03:32:44.364: E/AndroidRuntime(2682): at com.imooc.ui_basetestdemo_food.MainActivity.showPic(MainActivity.java:260)
08-01 03:32:44.364: E/AndroidRuntime(2682): at com.imooc.ui_basetestdemo_food.MainActivity.access$12(MainActivity.java:257)
08-01 03:32:44.364: E/AndroidRuntime(2682): at com.imooc.ui_basetestdemo_food.MainActivity$ButtonListener.onClick(MainActivity.java:225)
08-01 03:32:44.364: E/AndroidRuntime(2682): at android.view.View.performClick(View.java:4438)
08-01 03:32:44.364: E/AndroidRuntime(2682): at android.view.View$PerformClick.run(View.java:18422)
08-01 03:32:44.364: E/AndroidRuntime(2682): at android.os.Handler.handleCallback(Handler.java:733)
08-01 03:32:44.364: E/AndroidRuntime(2682): at android.os.Handler.dispatchMessage(Handler.java:95)
08-01 03:32:44.364: E/AndroidRuntime(2682): at android.os.Looper.loop(Looper.java:136)
08-01 03:32:44.364: E/AndroidRuntime(2682): at android.app.ActivityThread.main(ActivityThread.java:5001)
08-01 03:32:44.364: E/AndroidRuntime(2682): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 03:32:44.364: E/AndroidRuntime(2682): at java.lang.reflect.Method.invoke(Method.java:515)
08-01 03:32:44.364: E/AndroidRuntime(2682): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-01 03:32:44.364: E/AndroidRuntime(2682): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-01 03:32:44.364: E/AndroidRuntime(2682): at dalvik.system.NativeStart.main(Native Method)
08-01 03:32:44.368: D/dalvikvm(2682): GC_FOR_ALLOC freed 81K, 4% free 3718K/3856K, paused 2ms, total 3ms
错误提示?求解
正在回答
NullPointerException,MainActivity.java的260行,showPic出现了空指针异常!是不是这出了问题iv_pic.setImageResource(lists_get.get(count).getPic());?你需要先判断lists_get是不是为null,再判断lists_get.的长度是不是大于count?再判断lists_get.get(count).getPic()。祝:学习愉快
加上判断if(lists_get!=null && lists_get.size()>count && lists_get.get(count)!=null){,怎么跟”透明度88“提交的作业这么相似呢?并且他的作业中ImageView对象iv_pic没有初始化。所以报的空指针异常,看看你是不是犯的同样的错误。toggleButton.setOnClickListener(buttonListener);也是他作业中出现的问题,为toggleButton设置错了监听器。由于看到的代码有限,你可以自己根据log查代码,或者提交作业,查看老师回复的批复文档。祝:学习愉快
class ButtonListener implements OnClickListener { @Override public void onClick(View arg0) { switch (arg0.getId()) { case R.id.btn_find: // 当用户点击寻找时,需要筛查信息,并把结果显示在imageView上 lists_get.clear(); count = 0; checkData(); showPic(count); break; case R.id.tb_click: break; } } private void checkData() { // 找出菜品 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); } } System.out.println("*************" + lists_get.size() + "****************"); } } private void showPic(int count) { // count值为要显示lists_get列表中第几个菜品的图片 // count值从0开始,表示显示lists_get列表中第一次菜品的图片 if (lists_get.size() > count&&lists_get!=null) { iv_pic.setImageResource(lists_get.get(count).getPic()); } } }
08-01 05:26:21.444: I/System.out(3593): *************1****************
08-01 05:26:21.448: D/AndroidRuntime(3593): Shutting down VM
08-01 05:26:21.448: W/dalvikvm(3593): threadid=1: thread exiting with uncaught exception (group=0xa4cd7b20)
08-01 05:26:21.468: E/AndroidRuntime(3593): FATAL EXCEPTION: main
08-01 05:26:21.468: E/AndroidRuntime(3593): Process: com.imooc.ui_basetestdemo_food, PID: 3593
08-01 05:26:21.468: E/AndroidRuntime(3593): java.lang.NullPointerException
08-01 05:26:21.468: E/AndroidRuntime(3593): at com.imooc.ui_basetestdemo_food.MainActivity.showPic(MainActivity.java:219)
08-01 05:26:21.468: E/AndroidRuntime(3593): at com.imooc.ui_basetestdemo_food.MainActivity.access$12(MainActivity.java:215)
08-01 05:26:21.468: E/AndroidRuntime(3593): at com.imooc.ui_basetestdemo_food.MainActivity$ButtonListener.onClick(MainActivity.java:193)
08-01 05:26:21.468: E/AndroidRuntime(3593): at android.view.View.performClick(View.java:4438)
08-01 05:26:21.468: E/AndroidRuntime(3593): at android.view.View$PerformClick.run(View.java:18422)
08-01 05:26:21.468: E/AndroidRuntime(3593): at android.os.Handler.handleCallback(Handler.java:733)
08-01 05:26:21.468: E/AndroidRuntime(3593): at android.os.Handler.dispatchMessage(Handler.java:95)
08-01 05:26:21.468: E/AndroidRuntime(3593): at android.os.Looper.loop(Looper.java:136)
08-01 05:26:21.468: E/AndroidRuntime(3593): at android.app.ActivityThread.main(ActivityThread.java:5001)
08-01 05:26:21.468: E/AndroidRuntime(3593): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 05:26:21.468: E/AndroidRuntime(3593): at java.lang.reflect.Method.invoke(Method.java:515)
08-01 05:26:21.468: E/AndroidRuntime(3593): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
08-01 05:26:21.468: E/AndroidRuntime(3593): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
08-01 05:26:21.468: E/AndroidRuntime(3593): at dalvik.system.NativeStart.main(Native Method)
08-01 05:26:21.476: D/dalvikvm(3593): GC_FOR_ALLOC freed 81K, 4% free 3718K/3856K, paused 7ms, total 8ms
08-01 05:26:27.164: I/Process(3593): Sending signal. PID: 3593 SIG: 9
求解。。。
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7238 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星