我有一个片段中的列表,我想要一个新的活动开始,当我按下列表中的东西。我只试着用我的第一选择,但是当我尝试它,我得到的错误:ClassCastException:不能转换为android.app.Activity
03-20 15:16:13.017: E/AndroidRuntime(941): Caused by: java.lang.ClassCastException: com.test.test.Algebra cannot be cast to android.app.Activity
这里是我的片段类:
public class FragmentTest extends SherlockListFragment {
String formler[]= {
"Algebra",
"Aritmetik",
"Differential- och integralkalkyl",
"Funktioner",
"Geometri",
"Komplexa tal",
"Statistik och sannolikhet",
"Trigonometri"
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
setListAdapter(new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_1, formler));
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String choice = formler[position];
try{
@SuppressWarnings("rawtypes")
Class choiceClass = Class.forName("com.test.test." + choice);
Intent startIntent = new Intent(getActivity().getBaseContext(), choiceClass);
startActivity(startIntent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
}
我在做什么错?
为什么你想启动这样一个新的活动?使用开关,只需在您的意图中更改班级名称并启动它。不需要使用字符串数组,并做类似的东西 – hardartcore 2013-03-20 15:32:55
显示你的代码'com.test.test.Algebra'请 – Terry 2013-03-20 15:35:27
看起来代数不是一个活动...确保你的代数类扩展Activity ..也发表代数类 – Pragnani 2013-03-20 15:36:57