2

我在一个名为 的 LinearLayout 中有一个 ListView listView1,对于特定的行,我有一个 LinearLayout,里面有一个com.facebook.widget.ProfilePictureViewer和一个 TextView。我创建了一个CustomListAdapter来填充列表。我正在从Request.executeMyFriendsRequestAsync().

在研究了一些关于stackoverflow的类似帖子以及其他教程之后,我发布了这个。请帮忙,我从过去 1 天开始就一直坚持这一点。

list_item.xml

<com.facebook.widget.ProfilePictureView
    android:padding="2dp"
    android:id="@+id/friendProfilePicture"
    android:layout_width="43dp"
    android:layout_height="44dp" >
</com.facebook.widget.ProfilePictureView>
<TextView
    android:id="@+id/tv_friendName"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_gravity="center_vertical"
    android:paddingLeft="2dp"
    android:text="Name"/>

我想我要么将适配器设置错误,要么在适配器的构造函数中存在不匹配的内容。

我的适配器.java

public class MyAdapter extends ArrayAdapter<String> {

private final Context context;
private String[] names;
private String[] ids;

public MyAdapter(Context context, int layoutResourceId, String[] n, String[]i) {
    super(context, R.layout.list_item, n);
    this.context = context;
    names = n;
    ids = i;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View vi = inflater.inflate(R.layout.list_item, parent, false);
    TextView tvName = (TextView) vi.findViewById(R.id.tv_friendName);
    ProfilePictureView profilePic = (ProfilePictureView) vi.findViewById(R.id.profilepic);
    tvName.setText(names[position]);
    profilePic.setProfileId(ids[position]);
    return vi;
}
}

这是我设置适配器的代码,

public class MainFragment extends Fragment {

以及设置适配器的语句

context = MainActivity.context;

listView.setAdapter(new MyAdapter(context,R.layout.list_item, names, id));

MainActivty.java中有一个静态变量上下文类型Context

names 和 id 是两个具有值且不为空的字符串数组。

这是堆栈跟踪

07-15 07:02:07.729: E/AndroidRuntime(3367): FATAL EXCEPTION: main
07-15 07:02:07.729: E/AndroidRuntime(3367): java.lang.NullPointerException
07-15 07:02:07.729: E/AndroidRuntime(3367):     at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
07-15 07:02:07.729: E/AndroidRuntime(3367):     at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
07-15 07:02:07.729: E/AndroidRuntime(3367):     at azeem.anas.facebookloginbutton.MyAdapter.<init>(MyAdapter.java:19)
07-15 07:02:07.729: E/AndroidRuntime(3367):     at azeem.anas.facebookloginbutton.MainFragment$3.onCompleted(MainFragment.java:103)
07-15 07:02:07.729: E/AndroidRuntime(3367):     at com.facebook.Request$2.onCompleted(Request.java:285)
07-15 07:02:07.729: E/AndroidRuntime(3367):     at com.facebook.Request$4.run(Request.java:1240)
07-15 07:02:07.729: E/AndroidRuntime(3367):     at android.os.Handler.handleCallback(Handler.java:725)
07-15 07:02:07.729: E/AndroidRuntime(3367):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-15 07:02:07.729: E/AndroidRuntime(3367):     at android.os.Looper.loop(Looper.java:137)
07-15 07:02:07.729: E/AndroidRuntime(3367):     at android.app.ActivityThread.main(ActivityThread.java:5041)
07-15 07:02:07.729: E/AndroidRuntime(3367):     at java.lang.reflect.Method.invokeNative(Native Method)
07-15 07:02:07.729: E/AndroidRuntime(3367):     at java.lang.reflect.Method.invoke(Method.java:511)
07-15 07:02:07.729: E/AndroidRuntime(3367):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-15 07:02:07.729: E/AndroidRuntime(3367):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-15 07:02:07.729: E/AndroidRuntime(3367):     at dalvik.system.NativeStart.main(Native Method)

任何帮助将不胜感激。

4

2 回答 2

1

在适配器类的以下语句中,我犯了一个非常愚蠢的错误

ProfilePictureView profilePic = (ProfilePictureView) vi.findViewById(R.id.profilepic);,

我指的是错误的资源。其实是

ProfilePictureView profilePic = (ProfilePictureView) vi.findViewById(R.id.friendProfilePicture);


感谢所有为解决我的问题而努力的人

于 2013-07-16T05:46:57.957 回答
0
public class MyAdapter extends ArrayAdapter<String> {
   private String[] ids;

   public MyAdapter(Context context, int layoutResourceId, String[] n, String[] i) {
       super(context, R.layout.list_item, n);
       this.ids = i;
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
      View view = convertView;
      if (view == null) {
         LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
         view = inflater.inflate(layoutResourceId, null);
      }
      TextView tvName = (TextView) view.findViewById(R.id.tv_friendName);
      ProfilePictureView profilePic = (ProfilePictureView) view.findViewById(R.id.profilepic);
      tvName.setText(getItem(position));
      profilePic.setProfileId(ids[position]);
      return view;
   }
}

试试这个它应该工作。并保持context = getActivity();

listView.setAdapter(new MyAdapter(context,R.layout.list_item, names, id));重要提示:打电话onActivityCreateed()

于 2013-07-15T07:58:22.480 回答