user3388627
user3388627

Reputation: 1

This intent is giving me error "The constructor Intent(Character, Class) is undefined""

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

Answers (3)

Nambi
Nambi

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

Ken Wolf
Ken Wolf

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

nKn
nKn

Reputation: 13761

In this line:

String Character = classes[position];

Character is a reserved word for the type Character. You'll have to rename that variable.

Upvotes: 1

Related Questions