这个意图给了我错误"构造函数Intent(Character,Class)未定义""

时间:2014-04-12 16:23:44

标签: android android-intent undefined

我在意图中收到错误“构造函数意图(字符,类)未定义请告诉我解决方案!!!”

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);
}

3 个答案:

答案 0 :(得分:3)

您需要一个Activity实例来创建意图

Fragment可以使用getActivity()

访问Activity实例
Intent intent = new Intent(getActivity(), ourClass);
startActivity(intent);

答案 1 :(得分:2)

更改

Intent ourIntent = new Intent(Characters.this, ourClass);

Intent ourIntent = new Intent(getActivity(), ourClass);

您尝试使用的Intent constructorContext作为第一个参数,而您的Characters类不是Context,而Activity则是getActivity()主持它。

此活动可通过{{1}}

用于任何片段

答案 2 :(得分:1)

在这一行:

String Character = classes[position];

CharacterCharacter类型的保留字。您必须重命名该变量。