1 package com.moniter.helperclasses; 2 3 import java.util.List; 4 5 import android.content.Context; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.ViewGroup; 9 import android.widget.BaseAdapter; 10 import android.widget.ImageView; 11 import android.widget.TextView; 12 13 import com.hanvon.moniter.R; 14 15 public class SiteListAdapter extends BaseAdapter { 16 17 // 地点列表 18 private List<SiteInformation> sites = null; 19 private LayoutInflater mInflater; 20 21 // Constructor 22 public SiteListAdapter(Context context, List<SiteInformation> sites) { 23 this.sites = sites; 24 this.mInflater = LayoutInflater.from(context); 25 } 26 27 // 返回列表项数 28 @Override 29 public int getCount() { 30 return sites.size(); 31 } 32 33 @Override 34 public Object getItem(int position) { 35 return sites.get(position); 36 } 37 38 @Override 39 public long getItemId(int position) { 40 return position; 41 } 42 43 @SuppressWarnings("null") 44 @Override 45 public View getView(int position, View convertView, ViewGroup parent) { 46 // 取得位置position对应的site信息 47 SiteInformation siteInfo = sites.get(position); 48 49 //取得ListView的布局文件 50 convertView = mInflater.inflate(R.layout.site_list_item, null); 51 52 //初始化viewHolder 53 ViewHolder viewHolder = null; 54 55 //View的绑定 56 if (convertView == null) { 57 viewHolder = new ViewHolder(); 58 viewHolder.site_name = (TextView) convertView 59 .findViewById(R.id.name_site); 60 viewHolder.value_noise = (TextView) convertView 61 .findViewById(R.id.noise); 62 viewHolder.value_dust = (TextView) convertView 63 .findViewById(R.id.dust); 64 viewHolder.status_noise = (ImageView) convertView 65 .findViewById(R.id.color_indicator_noise); 66 viewHolder.status_dust = (ImageView) convertView 67 .findViewById(R.id.color_indicator_dust); 68 69 convertView.setTag(viewHolder); 70 } else { 71 viewHolder = (ViewHolder) convertView.getTag(); 72 } 73 74 //取得相应的值:抱错!!!!!为什么???? 75 viewHolder.site_name.setText("123"); //???siteInfo.getSiteName() 76 viewHolder.value_noise.setText(siteInfo.getNoiseValue()); 77 viewHolder.value_dust.setText(siteInfo.getDustValue()); 78 viewHolder.status_noise.setBackgroundResource(siteInfo.getNoiseStatus()); 79 viewHolder.status_dust.setBackgroundResource(siteInfo.getDustStatus()); 80 81 //返回Poistion对应的view 82 return convertView; 83 } 84 85 static class ViewHolder { 86 public TextView site_name; 87 public TextView value_noise; 88 public TextView value_dust; 89 public ImageView status_noise; 90 public ImageView status_dust; 91 } 92 }
老是在第75行报错。搞了一天了没有发现原因。
06-16 18:09:50.232: E/AndroidRuntime(10487): FATAL EXCEPTION: main
06-16 18:09:50.232: E/AndroidRuntime(10487): java.lang.NullPointerException
06-16 18:09:50.232: E/AndroidRuntime(10487): at com.moniter.helperclasses.SiteListAdapter.getView(SiteListAdapter.java:75)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.widget.AbsListView.obtainView(AbsListView.java:2040)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.widget.ListView.makeAndAddView(ListView.java:1772)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.widget.ListView.fillDown(ListView.java:672)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.widget.ListView.fillFromTop(ListView.java:732)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.widget.ListView.layoutChildren(ListView.java:1625)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.widget.AbsListView.onLayout(AbsListView.java:1870)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.view.View.layout(View.java:11418)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.view.ViewGroup.layout(ViewGroup.java:4328)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1652)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1510)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.widget.LinearLayout.onLayout(LinearLayout.java:1415)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.view.View.layout(View.java:11418)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.view.ViewGroup.layout(ViewGroup.java:4328)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1652)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1510)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.widget.LinearLayout.onLayout(LinearLayout.java:1415)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.view.View.layout(View.java:11418)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.view.ViewGroup.layout(ViewGroup.java:4328)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1652)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1510)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.widget.LinearLayout.onLayout(LinearLayout.java:1415)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.view.View.layout(View.java:11418)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.view.ViewGroup.layout(ViewGroup.java:4328)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:930)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.view.View.layout(View.java:11418)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.view.ViewGroup.layout(ViewGroup.java:4328)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.widget.FrameLayout.onLayout(FrameLayout.java:443)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.view.View.layout(View.java:11418)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.view.ViewGroup.layout(ViewGroup.java:4328)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1652)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1510)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.widget.LinearLayout.onLayout(LinearLayout.java:1415)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.view.View.layout(View.java:11418)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.view.ViewGroup.layout(ViewGroup.java:4328)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.widget.FrameLayout.onLayout(FrameLayout.java:443)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.view.View.layout(View.java:11418)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.view.ViewGroup.layout(ViewGroup.java:4328)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.os.Looper.loop(Looper.java:137)
06-16 18:09:50.232: E/AndroidRuntime(10487): at android.app.ActivityThread.main(ActivityThread.java:4441)
06-16 18:09:50.232: E/AndroidRuntime(10487): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 18:09:50.232: E/AndroidRuntime(10487): at java.lang.reflect.Method.invoke(Method.java:511)
06-16 18:09:50.232: E/AndroidRuntime(10487): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-16 18:09:50.232: E/AndroidRuntime(10487): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-16 18:09:50.232: E/AndroidRuntime(10487): at dalvik.system.NativeStart.main(Native Method)
最后只好求助.Stackoverflow发问题时网页经常出问题,只好改加QQ群,最后得到android学习交流群的“成都-Young”提点,终于解决问题:
第50行的
convertView
//取得ListView的布局文件 50 convertView = mInflater.inflate(com.hanvon.moniter.R.layout.site_list_item, null);
放到56行的if语句里面进行初始化,这样convertView在为Null的时候就能正常初始化,而不是每次都提前初始化一个值,就直接运行71行的代码。
55 //View的绑定
56 if (convertView == null) {convertView = mInflater.inflate(com.hanvon.moniter.R.layout.site_list_item, null);
}