我在意图中收到错误“构造函数意图(字符,类)未定义请告诉我解决方案!!!”
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);
}
答案 0 :(得分:3)
您需要一个Activity实例来创建意图
Fragment可以使用getActivity()
Intent intent = new Intent(getActivity(), ourClass);
startActivity(intent);
答案 1 :(得分:2)
更改
Intent ourIntent = new Intent(Characters.this, ourClass);
到
Intent ourIntent = new Intent(getActivity(), ourClass);
您尝试使用的Intent constructor将Context
作为第一个参数,而您的Characters
类不是Context
,而Activity
则是getActivity()
主持它。
此活动可通过{{1}}
用于任何片段答案 2 :(得分:1)