我創建了一個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。什麼問題呢?
好像是一般下面這麼寫吧,你的有點怪,而且你也少了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;
}