1

我得到一个空指针,lv1.setAdapter(myAdapter)我不知道为什么。有人能看出什么问题吗?

我正在尝试实现两行列表

我的活动.class

final ListView lv1 = (ListView) findViewById(R.id.ListView01);
          Log.d("",topicTitle.toString() + "----------" +  topicAuthor.toString()); //not null
          MyCustomBaseAdapter myAdapter =  new MyCustomBaseAdapter(DiscussionTopics.this, topicTitle , topicAuthor);
          lv1.setAdapter(myAdapter);

MyCustomBaseAdapter.class

public class MyCustomBaseAdapter extends BaseAdapter {
    private static ArrayList<String> topics;
    private static ArrayList<String> author;
    private LayoutInflater mInflater;
    public MyCustomBaseAdapter(Context context, ArrayList<String> topics, ArrayList<String> author ) {
        this.topics = topics;
        this.author = author;
        mInflater = LayoutInflater.from(context);
    }
    public long getItemId(int position) {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.custom_row_view, null);
            holder = new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.title);
            holder.author = (TextView) convertView.findViewById(R.id.author);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.title.setText(topics.get(position));
        holder.author.setText(author.get(position));
        return convertView;
    }
    static class ViewHolder {
        TextView title;
        TextView author;
    }
    public int getCount() {
        // TODO Auto-generated method stub
        return topics.size();
    }
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return topics.get(position);
    }
}
4

1 回答 1

1

所以你永远不会得到处理程序R.id.ListView01

final ListView lv1 = (ListView) findViewById(R.id.ListView01);

确保您在此行之前调用 setContentView() 并为其提供包含R.id.ListView01

于 2012-06-07T20:01:20.517 回答