简体   繁体   English

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

[英]NullPointerException in CustomListAdapter while setting the adapter from a Fragment

I have a ListView inside a LinearLayout named listView1 , and for a particular row, I have a LinearLayout inside which there is a com.facebook.widget.ProfilePictureViewer and a TextView. 我在名为listView1的LinearLayout中有一个ListView,对于特定的行,我有一个LinearLayout,其中有com.facebook.widget.ProfilePictureViewer和TextView。 I have created a CustomListAdapter to populate the List. 我创建了一个CustomListAdapter来填充列表。 I am setting the adapter from the Request.executeMyFriendsRequestAsync(). 我正在从Request.executeMyFriendsRequestAsync().设置适配器Request.executeMyFriendsRequestAsync().

I am posting this after researching in a number of similar post on the stackoverflow as well as other tutorials. 在研究了关于stackoverflow的许多类似帖子以及其他教程之后,我将其发布。 Please help, I am stuck on this from last 1 day. 请帮忙,我从最近1天开始一直对此感到困惑。

list_item.xml 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"/>

I think I am either setting the adapter wrong or have something mismatch in the adapter's constructor. 我认为我是在设置适配器错误或适配器的构造函数中有些不匹配。

MyAdapter.java 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;
}
}

and here is the code where I am setting the adapter, 这是我设置适配器的代码,

public class MainFragment extends Fragment {

and the statement for setting the adapter 以及设置适配器的声明

context = MainActivity.context;

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

There is a static variable context of type Context in the MainActivty.java, MainActivty.java中有一个类型为Context的静态变量上下文,

names and id are two String Arrays which are having values and are not null. name和id是两个具有值且不为null的字符串数组。

Here is the StackTrace 这是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)

Any help would greatly appreciated. 任何帮助将不胜感激。

A very silly mistake i have done, in the following statement in the Adapter class 我在Adapter类的以下语句中犯了一个非常愚蠢的错误

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

I was referring to a wrong resource. 我指的是错误的资源。 It was actually 实际上是

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


Thanks to all those who made efforts in solving my problem 感谢所有为解决我的问题而付出努力的人

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;
   }
}

Try this it should Work. 试试这个,它应该工作。 And keep context = getActivity(); 并保持context = getActivity();

Important : call listView.setAdapter(new MyAdapter(context,R.layout.list_item, names, id)); 重要事项:呼叫listView.setAdapter(new MyAdapter(context,R.layout.list_item, names, id)); in onActivityCreateed() onActivityCreateed()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM