Search code examples
javaandroidxmllistviewbaseadapter

Android - findViewById(int id) always returns null


I have created CreateEvent activity

        @Override
        public void onClick(View v) {
            Intent in = new Intent(UserActivity.this, CreateEvent.class);
            startActivity(in);
        }
    });

In CreateEvent activity, I want to create listView and here is my code for it:

@Override
public void friendsDownlaoded(ArrayList<User> friends) {
    this.friends = friends;
    this.adapter = new  InviteFriendAdapter(getLayoutInflater());
    this.friendListview.setAdapter(adapter);
    removeRoation();    
    setUpContent();
}

and here is code for InviteFriendAdapter, which is inner class and it can see ArrayList friends:

private class InviteFriendAdapter extends BaseAdapter {
    private LayoutInflater inflater;

    public InviteFriendAdapter(LayoutInflater inflater) {
        this.inflater = inflater;
    }

    @Override
    public int getCount() {
        return friends.size();
    }

    @Override
    public Object getItem(int i) {
        return friends.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View myView, ViewGroup vg) {
        FriendHolder frndHolder;
        User user = friends.get(position);


        if (myView == null) {
            myView = this.inflater.inflate(R.layout.invite_friends_event_item, null,false);
            ImageView img= (ImageView) findViewById(R.id.inviteCheckBox);
            frndHolder = new FriendHolder();
            frndHolder.hisImage = (ImageView) findViewById(R.id.inviteCheckBox);
            frndHolder.hisNumber = (TextView) findViewById(R.id.inviteHisNumber);
            frndHolder.hisName = (TextView) findViewById(R.id.inviteHisName);

            myView.setTag(frndHolder);
        } else {
            frndHolder = (FriendHolder) myView.getTag();
        }


        frndHolder.hisName.setText("aa");
        frndHolder.hisNumber.setText("uUU");

        return myView;
    }

}

private static class FriendHolder {
    TextView hisName;
    TextView hisNumber;
    ImageView hisImage;
}

I've checked ArrayList friends and it isn't null; The program throws NullPointException at adapters getView() and the reason is that when myView == null - (ImageView) findViewById(R.id.inviteCheckBox); returns null

Here is xml file for item:

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

    <TextView
        android:id="@+id/inviteHisName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_margin="@dimen/possible_his_number_margin"
        android:textSize="@dimen/possible_his_name_text" />

    <TextView
        android:id="@+id/inviteHisNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/inviteHisName"
        android:layout_margin="@dimen/possible_his_number_margin"
        android:textSize="@dimen/possible_his_number_text" />

    <ImageView
        android:id="@+id/inviteCheckBox"
        android:layout_width="@dimen/smile_pic_width"
        android:layout_height="@dimen/smile_pic_height"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_margin="@dimen/possible_check_box_margin"
        android:contentDescription="@string/image"
        android:scaleType="fitXY" />

</RelativeLayout>

I wasn't able to find solution on web. I've created adapter several times and it worked good, but now.. Can anybody tell me why findViewById() returns null? Thank you in advance


Solution

  • In an adapter getView(), call findViewById() on the view you inflated, not on the activity view hierarchy (in case the adapter is a non-static inner class in an activity).

    In your code, replace findViewById() with myView.findViewById().