O_Casper_ne
2013-04-22 08:43
采纳率: 100%
浏览 2.2k

当设置 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条回答 默认 最新

相关推荐 更多相似问题