2013-02-27 63 views
1

我试图填充listview,数据通过创建它的意图传递给活动。您看到的println声明确认数据正确传递(即打印了预期内容,这意味着适配器中引用的ArrayList已正确初始化)。不过,我一直就行了NullPointerException在填充BaseAdapter中的数据时

content.setText(Html.fromHtml(cmts.get(position).content)); 

一定有什么东西错在适配器得到一个NullPointerException - 也许在getItem()时,或许我对cmts.get(position)电话是不是做我觉得是,但在这一点上,我无法弄清楚。

public class CommentsView extends Activity { 
ArrayList<Comment> cmts; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_comments_view); 

    cmts = (ArrayList<Comment>) getIntent().getExtras().getSerializable("clist"); 

    for (Comment c : cmts) { 
     System.out.println("CMTinCV: " + c.content); 
    } 

    ListView lv = (ListView)findViewById(R.id.commentsList); 

    CommentAdapter ca = new CommentAdapter(); 

    lv.setAdapter(ca); 

} 

class CommentAdapter extends BaseAdapter { 
    public CommentAdapter(){ 

    } 
@Override 
public int getCount() { 
    return cmts.size()-1; 
} 

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

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

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

    if (convertView == null) { 
     convertView = getLayoutInflater().inflate(R.layout.commentbox, null); 

     TextView content = (TextView)findViewById(R.id.commentText); 
     TextView author = (TextView)findViewById(R.id.commentAuthor);    
     TextView date = (TextView)findViewById(R.id.commentDate);  

     content.setText(Html.fromHtml(cmts.get(position).content)); 
     author.setText(cmts.get(position).author.name); 
     date.setText(cmts.get(position).date); 
    } 

    return convertView; 
} 

} 
} 
+0

你必须使用convertView来检索内容,作者,日期。 – Blackbelt 2013-02-27 08:59:23

回答

2

适配器的构造函数改成这样(如果它不是为您的活动的内部类):

ArrayList<Comment> cmts; 
public CommentAdapter(ArrayList<Comment> mComments){ 
    this.cmts = mComments; 
} 

和这些行:

TextView content = (TextView)findViewById(R.id.commentText); 
TextView author = (TextView)findViewById(R.id.commentAuthor);    
TextView date = (TextView)findViewById(R.id.commentDate); 

应该是这样的:

TextView content = (TextView) convertView.findViewById(R.id.commentText); 
TextView author = (TextView) convertView.findViewById(R.id.commentAuthor);    
TextView date = (TextView) convertView.findViewById(R.id.commentDate); 
+0

我曾经这样做过,并得到相同的结果。我会再试一次,但请注意,在调用适配器之前,ArrayList已经被定义为一个实例变量,所以我不应该这样做,是吗? – drewmoore 2013-02-27 08:47:53

+0

如果baseadapter类是在同一个文件中声明的,我不认为它需要传递,但是如果它在另一个文件中而不是它应该。我建议你找出第二件事实际上是空的,也许你的某个视图是空的。尝试调试并找出 – hardartcore 2013-02-27 08:50:11

+0

是的,它被定义为内部类。试图现在调试它 – drewmoore 2013-02-27 08:52:21

0

检查变量为空或不是第一:

if(cmts.get(position) != null) { 
    content.setText(Html.fromHtml(cmts.get(position).content)); 
    author.setText(cmts.get(position).author.name); 
    date.setText(cmts.get(position).date); 
} 
+0

我知道这可能是一个最佳实践,所以我把它放在那里,但是a)据我了解,'getCount()'方法将ViewGroup的大小限制为'cmts arraylist'中的项数减1 。因此,'cmts.get(position)'可能为null会怎么样呢?我要求教育目的... – drewmoore 2013-02-27 08:51:16

3

您需要访问的TextView在下面的getview方法: convertView.findViewById(R.id.commentText);访问它像这样。

@Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    if (convertView == null) { 
    convertView = getLayoutInflater().inflate(R.layout.commentbox, null); 

    TextView content = (TextView)convertView.findViewById(R.id.commentText); 
    TextView author = (TextView)convertView.findViewById(R.id.commentAuthor);    
    TextView date = (TextView)convertView.findViewById(R.id.commentDate);  

    content.setText(Html.fromHtml(cmts.get(position).content)); 
    author.setText(cmts.get(position).author.name); 
    date.setText(cmts.get(position).date); 
} 
    return convertView; 
} 
0

要创建这样

CommentAdapter ca = new CommentAdapter(cmts); 

而且CommentAdapter类适配器这样

public CommentAdapter(ArrayList<Comment> cmts){ 
     this.cmts = cmts; 
} 

创建构造函数,并在CommentAdapter类中创建局部变量

private ArrayList<Comment> cmts; 
+0

不,它不。我已经将ArrayList定义为实例变量,并在调用适配器之前将其初始化 – drewmoore 2013-02-27 09:00:15