从Fragment设置适配器时CustomListAdapter中的NullPointerException

时间:2013-07-15 07:06:58

标签: android

我在一个名为listView1的LinearLayout中有一个ListView,对于一个特定的行,我有一个LinearLayout,里面有一个com.facebook.widget.ProfilePictureViewer和一个TextView。我创建了一个CustomListAdapter来填充List。 我正在从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"/>

我认为我要么设置适配器错误,要么在适配器的构造函数中出现不匹配的情况。

MyAdapter.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是两个字符串数组,它们具有值且不为空。

这是StackTrace

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)

任何帮助都会非常感激。

2 个答案:

答案 0 :(得分:1)

我已经完成了一个非常愚蠢的错误,在Adapter类的以下语句中

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

我指的是错误的资源。实际上是

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


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

答案 1 :(得分: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()