krzk
krzk

Reputation: 431

adapter in fragment - android.app.Application cannot be cast to android.app.Activity

so now we've got a

CustomAdapter adapter = new CustomAdapter(getActivity().getApplicationContext(), R.layout.list_style, RowBean_data);
ListView lista = (ListView) rootView.findViewById(R.id.lista);
lista.setAdapter(adapter);

in fragment, we want it to show a list with items and text.

Right now we're getting an error with the following code: http://pastebin.com/BV9X6Dys

The other classes:

CustomAdapter.java http://pastebin.com/eA5Xx1Ng

MainActivity.java pastebin.com/Nv1Zip11

HomeFragment.java pastebin.com/8DiXQHsy

How can we make it work?

Upvotes: 1

Views: 773

Answers (1)

Emil
Emil

Reputation: 2806

You are casting Context to Activity.

The error is here..

 LayoutInflater inflater = ((Activity)context).getLayoutInflater();

So the solution is to use the context to get the inflator service, like this

LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

Another alternate way is that like this

LayoutInflater inflater = LayoutInflater.from(context);

Like @Zharf said

EDIT

Also I think that passing getActivity() to the CustomAdapter's constructor is enough.

CustomAdapter adapter = new CustomAdapter(getActivity(), R.layout.list_style, RowBean_data);

Upvotes: 2

Related Questions