我创建了一个自定义列表视图。它有一个图像,文本和一个复选框,当我尝试设置上面的值时,我得到一个NUll指针异常。如果我没有设置值,我会得到默认列表......为什么会发生这种情况?
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 ..为什么?
答案 0 :(得分:1)
尝试使用空检查来围绕该行。这应该可以解决问题,
if(holder.tv!=null)
{
holder.tv.setText(""+pinfo.appname);
}
这不会对逻辑造成任何伤害。
答案 1 :(得分:0)
我认为您在任何其他ui中使用 * “textView1” * named id。更改TextView的ID名称并使用它。
检查此代码
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
View customView = inflater.inflate(R.layout.listrow, parent,false);
ImageView iv = (ImageView)customView.findViewById(R.id.imageView1);
TextView tv= (TextView) customView.findViewById(R.id.textView1);
CheckBox cb= (CheckBox)customView.findViewById(R.id.checkBox1);
PInfo pinfo = installedApps.get(arg0);
tv.setText(""+pinfo.appname);//If u comment this line it works
return customView;
}
使用此代码代替。可以解决你的问题。