Reputation: 1
I am getting error at the Intent "The constructor Intent(Character, Class) is undefined Please tell me a solution!!!"
public class Characters extends ListFragment {
String classes[] = {"Desmond", "Altair", "Ezio", "Connor", "Haytham", "Edward", "Aveline", "Lucy", "William", "Shaun", "Rebecca", "Clay", "Vidic",
"Tabs"};
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String Character = classes[position];
try{
Class ourClass = Class.forName("com.frost.assassinswikia." + Character);
Intent ourIntent = new Intent(Characters.this, ourClass);
startActivity(ourIntent);
}catch (ClassNotFoundException e){
e.printStackTrace();
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, classes);
setListAdapter(adapter);
}
Upvotes: 0
Views: 151
Reputation: 12042
You need a Activity instance to create the intent
Fragment can access the Activity instance with getActivity()
Intent intent = new Intent(getActivity(), ourClass);
startActivity(intent);
Upvotes: 3
Reputation: 23269
Change
Intent ourIntent = new Intent(Characters.this, ourClass);
to
Intent ourIntent = new Intent(getActivity(), ourClass);
The Intent constructor you are trying to use takes a Context
as the first parameter and your Characters
class is not a Context
, whereas the Activity
that hosts it is.
This activity is available to any fragment via getActivity()
Upvotes: 2