适配器中的getView nullPointerException

时间:2014-11-03 17:47:33

标签: java android android-listview nullpointerexception android-adapter

我有一个扩展基础适配器的适配器,但我不知道为什么它总是返回空指针异常,我不知道为什么... 这是我的代码:

public class MembersAdapter extends BaseAdapter {

    private static LayoutInflater inflater = null;
    private Activity activity;
    private ArrayList<UserInfoVO> data;

    public MembersAdapter(Activity a, ArrayList<UserInfoVO> data) {
        activity = a;

        this.data = data;
        inflater = (LayoutInflater) activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

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

    public View getView(final int position, View convertView,
                        final ViewGroup parent) {
        View vi = convertView;
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.members_list_item, null);
            holder = new ViewHolder();
            holder.userImage = (CXRemoteImageView) convertView.findViewById(R.id.members_item_pic);

            holder.title = (TextView) convertView.findViewById(R.id.members_item_title);
            holder.small_text = (TextView) convertView.findViewById(R.id.members_item_small_text);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Log.v("mtag","The url ="+data.get(position).getPictureUrl());
        holder.userImage.loadImmediate(data.get(position).getPictureUrl());
        holder.title.setText(data.get(position).getName());
        holder.small_text.setText(data.get(position).getAboutMe());

        return vi;
    }

    static class ViewHolder {
        public CXRemoteImageView userImage;
        public TextView title;
        public TextView small_text;
    }

}

正如我在日志中看到的,所有数据都传递给适配器,但为什么我会遇到这个问题。

这也是我的logCat输出:

致命的例外:主要

 java.lang.NullPointerException
            at com.example.adapters.MembersAdapter.getView(MembersAdapter.java:69)
            at android.widget.AbsListView.obtainView(AbsListView.java:2040)
            at android.widget.ListView.makeAndAddView(ListView.java:1772)
            at android.widget.ListView.fillDown(ListView.java:672)
            at android.widget.ListView.fillFromTop(ListView.java:732)
            at android.widget.ListView.layoutChildren(ListView.java:1625)
            at android.widget.AbsListView.onLayout(AbsListView.java:1870)
            at android.view.View.layout(View.java:11418)
            at android.view.ViewGroup.layout(ViewGroup.java:4328)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:443)
            at android.view.View.layout(View.java:11418)
            at android.view.ViewGroup.layout(ViewGroup.java:4328)
            at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1594)
            at android.view.View.layout(View.java:11418)
            at android.view.ViewGroup.layout(ViewGroup.java:4328)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:930)
            at android.view.View.layout(View.java:11418)
            at android.view.ViewGroup.layout(ViewGroup.java:4328)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:443)
            at android.view.View.layout(View.java:11418)
            at android.view.ViewGroup.layout(ViewGroup.java:4328)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1652)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1510)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1415)
            at android.view.View.layout(View.java:11418)
            at android.view.ViewGroup.layout(ViewGroup.java:4328)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:443)
            at android.view.View.layout(View.java:11418)
            at android.view.ViewGroup.layout(ViewGroup.java:4328)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
            at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4441)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
            at dalvik.system.NativeStart.main(Native Method)

第69行就是这一行holder.userImage.loadImmediate(data.get(position).getPictureUrl()); 注意:我看到它在设置图像源时返回nullPointerException,或者在textView上返回文本。

这是我的xml布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.clipix.clipix.views.CXRemoteImageView
        android:id="@+id/community_clips_item_pic"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="10dp"
        android:src="@drawable/default_user_image" />

    <TextView
        android:id="@+id/community_clips_item_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/community_clips_item_pic"
        android:layout_marginBottom="5dp"
        android:layout_toEndOf="@+id/community_clips_item_pic"
        android:layout_toRightOf="@+id/community_clips_item_pic"
        android:text="New Text" />

    <TextView
        android:id="@+id/community_clips_item_small_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@+id/community_clips_item_pic"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:textSize="12sp"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/community_clips_item_pic" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

您的布局和代码中的视图ID不同。例如community_clips_item_picmembers_item_pic。因此findViewById()返回null,并使用空值初始化视图持有者。

此外,适配器getView()必须始终返回非空View。考虑返回convertView,而不是vi,可以将其初始化为null。

答案 1 :(得分:0)

您的观看者的xml不包含您正在使用的ID:

members_item_pic, members_item_small_text, members_item_title.

您正在使用

community_clips_item_small_text,community_clips_item_title,community_clips_item_pic

代替..

更改您的ID,您将解决问题