Darko Petkovski
Darko Petkovski

Reputation: 3912

getView nullPointerException in adapter

I have a adapter that extends base adapter but I dont know why it always returning null pointer exception and I dont know why... Here is my code:

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

}

As i see in the logs all data is passed to the adapter, but why do I get this issue.

Also here is my logCat output:

FATAL EXCEPTION: main

 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)

line 69 is this one holder.userImage.loadImmediate(data.get(position).getPictureUrl()); Note: As I see its returning nullPointerException when setting image source, or text on a textView.

and here is my xml layout:

<?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>

Upvotes: 0

Views: 1028

Answers (2)

laalto
laalto

Reputation: 152797

The view ids in your layout and code are different. For example community_clips_item_pic vs. members_item_pic. Therefore findViewById() returns null and your view holder is initialized with nulls.

Also, an adapter getView() must always return a non-null View. Consider returning convertView, not vi that can be initilized to null.

Upvotes: 1

Pedro Oliveira
Pedro Oliveira

Reputation: 20500

Your view's xml doens't contain the Id's you're using:

members_item_pic, members_item_small_text, members_item_title.

You're using

community_clips_item_small_text,community_clips_item_title,community_clips_item_pic

instead..

Change your ids and you will solve the problem

Upvotes: 0

Related Questions