Mark Korzhov
Mark Korzhov

Reputation: 2159

Can't get access to the Activity item in onItemClick method

I have a Fragment where I get access to the m_items-layout:

public class MoviesListFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.m_items, container, false);

I created a custom adapter (extending the BaseAdapter class) that uses another layout as each list item belonging to the m_items-layout:

listView = (ListView) view.findViewById(R.id.listView);
MovieAdapter adapter = new MovieAdapter(getActivity(), initData());
listView.setAdapter(adapter);

On the layout, which uses the adapter, there is the textView with the identifier @+id/textID. I'm trying to set text to it in onItemClick-method:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        TextView movie_id = (TextView) view.findViewById(R.id.textID);
        movie_id.setText("577");
    }
});

but get an exception java.lang.NullPointerException here: movie_id.setText("577");

As I understand it, view.findViewById(R.id.textID); returns null.

Why is this happening?

Thanks.

Upvotes: 0

Views: 275

Answers (1)

laalto
laalto

Reputation: 152867

The View argument to onItemClick() corresponds to the View you returned from your adapter's getView().

To find a child view from it, make sure the view you return from getView() in fact contains a view with the given id (textID).

Upvotes: 1

Related Questions