user340
user340

Reputation: 383

Custom listview, error when setText

I have created a custom listview. It has a image, text and a checkbox and when I try to set a value for the above I get a NUll Pointer Exception. How ever if I don't set a value I get the default list...why is this happening?

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;

}

}

EDIT : aded the full source

It looks like as if findViewById(R.id.textView1) is returning null..why??

Upvotes: 0

Views: 642

Answers (2)

Shalini
Shalini

Reputation: 1733

I think you use *"textView1"*named id in anyother ui. Change the id name of the TextView and use it.

Check this code

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;
}

Use this code instead of that. May it solves your problem.

Upvotes: 0

Andro Selva
Andro Selva

Reputation: 54322

Try surrouding that line with a null check. This should do the trick,

if(holder.tv!=null)
 {
 holder.tv.setText(""+pinfo.appname);
 }

This won't cause any harm to the logic.

Upvotes: 1

Related Questions