pradeep
pradeep

Reputation: 3095

Listview.setadapter raising nullpointer exception

The code is as below

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent i = getIntent();

        ArrayList<String> imageUrl = i.getStringArrayListExtra("ImageUrl");
        String[] companyImageUrl = imageUrl.toArray(new String[0]);
            ListView companyList = null;
            LazyAdapter companyListViewAdapter;

            companyList = (ListView)findViewById(R.id.companylist);

            companyListViewAdapter = new LazyAdapter(companyListView.this , companyImageUrl);

            try {
                companyList.setAdapter(companyListViewAdapter);//exception here
            } catch (Exception e) {
                System.out.println(e);
            }
            setContentView(R.layout.company_list);
}

Edit: Lazyadapter code added

public class LazyAdapter extends BaseAdapter {

private Activity activity;
private String[] data;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader; 

public LazyAdapter(Activity a, String[] d) {
    activity = a;
    data=d;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader= new ImageLoader(activity.getApplicationContext());
}

public int getCount() {
    return data.length;
}

public Object getItem(int position) {
    return position;
}

public long getItemId(int position) {
    return position;
}

public static class ViewHolder{
    public TextView text;
    public ImageView image;
}

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    ViewHolder holder;
    UrlList urlList;

    urlList = XMLParser.urlList;

    if(convertView==null){
        vi = inflater.inflate(R.layout.item, null);
        holder=new ViewHolder();
        holder.text=(TextView)vi.findViewById(R.id.text);;
        holder.image=(ImageView)vi.findViewById(R.id.image);
        vi.setTag(holder);
    }
    else
        holder=(ViewHolder)vi.getTag();

    String[] categoryNameStrings = urlList.getCategoryName().toArray(new String[0]);

    holder.text.setText(categoryNameStrings[position]);
    holder.image.setTag(data[position]);
    imageLoader.DisplayImage(data[position], activity, holder.image);
    return vi;
}

}

Upvotes: 2

Views: 5206

Answers (4)

Senthil
Senthil

Reputation: 1244

when getting error like this first need to ensure that have given correct xml file as

setContentView(R.layout.main) and

listview id (i.e) findViewById(R.id.listview1);

Upvotes: 1

longhairedsi
longhairedsi

Reputation: 3136

what is this ?

 UrlList urlList;
 urlList = XMLParser.urlList;

and what is it doing here?

String[] categoryNameStrings = urlList.getCategoryName().toArray(new String[0]);

Can you please post the error log?

Upvotes: 0

Tanmay Mandal
Tanmay Mandal

Reputation: 40168

Use setContentView(R.layout.company_list);aftersuper.onCreate(savedInstanceState);

Upvotes: 5

ingsaurabh
ingsaurabh

Reputation: 15269

put this line after the super.onCreate(savedInstanceState);

setContentView(R.layout.company_list);

Upvotes: 2

Related Questions