2 o casper ne O_Casper_ne 于 2013.04.22 16:43 提问

当设置 setText 时 Custom listview 出错

我创建了一个Custom listview,有一个image, text 和 checkbox ,当我想设置一个值时,却获得一个NUll Pointer Exception异常。如果我不设置一个值如何获得默认的list?

class PInfo {
    String appname = "";
    String pname = "";
    String versionName = "";
    int versionCode = 0;
    int icon;
 }
 public class InstalledApps extends Activity{
    private static LayoutInflater inflater=null;
private ListView listview;
private Activity activity;
List<PInfo> installedApps = new ArrayList<PInfo>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.applist);
    activity = this;
    listview = (ListView)findViewById(R.id.listView1);
    PackageManager pm = getPackageManager();
    List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
    for(ApplicationInfo app : packages) {
        PInfo newInfo = new PInfo();
            newInfo.appname = (String) app.loadLabel(pm);
            newInfo.pname = app.packageName;
        if((app.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) == 1){             
            installedApps.add(newInfo);
        } else if ((app.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
            installedApps.add(newInfo);
        } else {
            installedApps.add(newInfo); 
        }
    }
    InstalledAppsAdapter iap = new InstalledAppsAdapter(this, installedApps);
listview.setAdapter(iap);
}
}
class InstalledAppsAdapter extends BaseAdapter{
private Activity activity;
private static LayoutInflater inflater=null;
List<PInfo> installedApps = new ArrayList<PInfo>();
ListView listview; 

public InstalledAppsAdapter(Activity a, List<PInfo> b) {
   // TODO Auto-generated constructor stub
    installedApps = b;
    activity = a;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    listview = (ListView)activity.findViewById(R.id.listView1);
}
@Override
public int getCount() {
    // TODO Auto-generated method stub
    return installedApps.size();
}
@Override
public Object getItem(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}
@Override
public long getItemId(int arg0) {
    // TODO Auto-generated method stub
    return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
    // TODO Auto-generated method stub
    View customView = arg1;
    ViewHolder holder;
    if(arg1 == null){
        customView = inflater.inflate(R.layout.listrow, null);
    holder = new ViewHolder();
    holder.iv = (ImageView)customView.findViewById(R.id.imageView1);
    holder.tv = (TextView) customView.findViewById(R.id.textView2);
    holder.cb = (CheckBox)customView.findViewById(R.id.checkBox1);
    }else{
        holder = (ViewHolder)customView.getTag();
    }
    PInfo pinfo = installedApps.get(arg0);
    holder.tv.setText(""+pinfo.appname);
    return customView;
}
static class ViewHolder{
    ImageView iv;
    CheckBox cb;
    TextView tv;
}

看起来像findViewById(R.id.textView1)返回null。什么问题呢?

2个回答

baimy1985
baimy1985   2013.04.22 21:37
已采纳

好像是一般下面这么写吧,你的有点怪,而且你也少了convertView.setTag(holder);这个动作

 public View getView(int position, View convertView, ViewGroup parent) {
            System.out.println("getView " + position + " " + convertView);
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item1, null);
                holder = new ViewHolder();
                holder.textView = (TextView)convertView.findViewById(R.id.text);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder)convertView.getTag();
            }
            holder.textView.setText(mData.get(position));
            return convertView;
        }
oSanYeCao1234567
oSanYeCao1234567   Ds   Rxr 2016.07.28 10:04

少了convertView.setTag(holder);这行代码,不然holder = (ViewHolder)convertView.getTag();,你获得的会是空的holder

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!