我的项目中有一个弹出窗口。我在列表视图中有一组帖子。如果我单击列表视图中的帖子,则会打开一个弹出窗口,其中包括帖子、评论和喜欢;用户可以在该窗口中为该帖子点赞并发表评论。但我无法将我的评论数据绑定到弹出窗口的列表视图。以下是我的代码和 logcat 数据:
ListView lstcomment = (ListView)popup.getContentView().findViewById(R.id.lstcomments);
CommentAdapter cmm = new CommentAdapter(this, commentarName, commentarPicture, commentarComment);
System.out.println(cmm.getCount());
if(cmm.getCount()>0)
{
lstcomment.setAdapter(cmm);
}
else
{
lstcomment.setVisibility(View.GONE);
nocomment.setVisibility(View.VISIBLE);
}
日志猫
08-25 02:54:27.680: E/AndroidRuntime(21581): FATAL EXCEPTION: main
08-25 02:54:27.680: E/AndroidRuntime(21581): java.lang.NullPointerException
08-25 02:54:27.680: E/AndroidRuntime(21581): at com.invemo.vodafonelivecoursetest.CommentAdapter.getCount(CommentAdapter.java:39)
08-25 02:54:27.680: E/AndroidRuntime(21581): at com.invemo.vodafonelivecoursetest.MainActivity.showFbPopup(MainActivity.java:1342)
08-25 02:54:27.680: E/AndroidRuntime(21581): at com.invemo.vodafonelivecoursetest.MainActivity.access$9(MainActivity.java:1297)
08-25 02:54:27.680: E/AndroidRuntime(21581): at com.invemo.vodafonelivecoursetest.MainActivity$3.onItemClick(MainActivity.java:343)
08-25 02:54:27.680: E/AndroidRuntime(21581): at android.widget.AdapterView.performItemClick(AdapterView.java:315)
08-25 02:54:27.680: E/AndroidRuntime(21581): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
08-25 02:54:27.680: E/AndroidRuntime(21581): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
08-25 02:54:27.680: E/AndroidRuntime(21581): at android.widget.AbsListView$1.run(AbsListView.java:3168)
08-25 02:54:27.680: E/AndroidRuntime(21581): at android.os.Handler.handleCallback(Handler.java:605)
08-25 02:54:27.680: E/AndroidRuntime(21581): at android.os.Handler.dispatchMessage(Handler.java:92)
08-25 02:54:27.680: E/AndroidRuntime(21581): at android.os.Looper.loop(Looper.java:137)
08-25 02:54:27.680: E/AndroidRuntime(21581): at android.app.ActivityThread.main(ActivityThread.java:4425)
08-25 02:54:27.680: E/AndroidRuntime(21581): at java.lang.reflect.Method.invokeNative(Native Method)
08-25 02:54:27.680: E/AndroidRuntime(21581): at java.lang.reflect.Method.invoke(Method.java:511)
08-25 02:54:27.680: E/AndroidRuntime(21581): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-25 02:54:27.680: E/AndroidRuntime(21581): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
08-25 02:54:27.680: E/AndroidRuntime(21581): at dalvik.system.NativeStart.main(Native Method)
如果我通过 System.out.println 将我的评论数据写入 logcat,我可以获得数据,但我无法将它们写入 listview。
我的适配器的代码:
package com.invemo.vodafonelivecoursetest;
public class CommentAdapter extends BaseAdapter {
private Activity activity;
private String[] profilePicture;
private String[] contentMessage;
private String[] sender;
private static LayoutInflater inflater=null;
public ImageLoader imageLoader;
public CommentAdapter(Activity a, String[] senders, String[] profilePictures, String[] announceContents) {
activity = a;
this.profilePicture=profilePictures;
this.contentMessage=announceContents;
this.sender=senders;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader=new ImageLoader(activity.getApplicationContext());
}
@Override
public int getCount() {
return sender.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public static class ViewHolder {
public TextView textTitle;
public ImageView image;
public TextView contentArea;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if (convertView == null) {
vi = inflater.inflate(R.layout.announce_item, null);
holder = new ViewHolder();
holder.textTitle = (TextView) vi.findViewById(R.id.textView1);
holder.image = (ImageView) vi.findViewById(R.id.imageView1);
holder.contentArea=(TextView)vi.findViewById(R.id.textView2);
vi.setTag(holder);
} else
holder = (ViewHolder) vi.getTag();
holder.textTitle.setText(sender[position]);
holder.contentArea.setText(contentMessage[position]);
holder.contentArea.setAutoLinkMask(Linkify.WEB_URLS);
holder.image.setTag(profilePicture[position]);
imageLoader.DisplayImage(profilePicture[position], activity, holder.image);
return vi;
}
}