Null pointer exception on Android – setadapter()

I'm trying to create a custom listview adapter (roughly based on this tutorial, but the example is simple, and I doubt you need to see it)

NewsEntriesAdapter.java

public class NewsEntriesAdapter extends BaseAdapter {
    private static ArrayList<NewsEntries> newsEntriesArrayList;

    private final LayoutInflater mInflater;

    public NewsEntriesAdapter(Context context, ArrayList<NewsEntries> entries) {
        newsEntriesArrayList = entries;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return newsEntriesArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return newsEntriesArrayList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.assignment_list_row_view,
                    null);
            holder = new ViewHolder();
            holder.txtName = (TextView) convertView.findViewById(R.id.name);
            holder.txtCityState = (TextView) convertView
                    .findViewById(R.id.cityState);
            holder.txtPhone = (TextView) convertView.findViewById(R.id.phone);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.txtName.setText(newsEntriesArrayList.get(position).getName());
        holder.txtCityState.setText(newsEntriesArrayList.get(position)
                .getCityState());
        holder.txtPhone.setText(newsEntriesArrayList.get(position).getPhone());

        return convertView;
    }

    static class ViewHolder {
        TextView txtName;
        TextView txtCityState;
        TextView txtPhone;
    }

}

Mainactivity.java (oncreate method)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ArrayList<NewsEntries> results = new ArrayList<NewsEntries>();

    // I left the NewsEntries class out of this question; it's just a POJO with some get and set methods for Name, CityState, and Phone
    NewsEntries sr = new NewsEntries();
    sr.setName("Justin Schultz");
    sr.setCityState("San Francisco, CA");
    sr.setPhone("415-555-1234");
    results.add(sr);

    final ListView lv = (ListView) findViewById(R.id.list);

    Sy@R_301_2354@.out.println(results.get(0).getName());

    lv.setAdapter(new NewsEntriesAdapter(this, results));
}

However, the upper lv.setadapter line will crash the application and return a null pointer exception

Any ideas?

resolvent:

You have not set anything in the activity. There are no setcontentview or addcontentview calls in oncreate

Therefore, findviewbyid will not find the view because there is no view at all and will return null

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>