ClassCastException异常:不能转换为android.app.Activity [英] ClassCastException: cannot be cast to android.app.Activity
本文介绍了ClassCastException异常:不能转换为android.app.Activity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个片段的名单,我想一个新的活动启动时,我美元的列表P $ PSS的东西。我只试着用我的第一选择,但是当我尝试它,我得到的错误:
I have a list in a fragment and I want a new activity to start when I press something in that list. I have only tried with my first option but when I try it I get the error:
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();
}
}
}
我在做什么错了?
推荐答案
确保代数是扩展的活动,你也可以简单的发起这样的新意图(currectActivity.this,代数的活动。类);
Make sure that Algebra is extending an activity and also you can simple initiate an activity like this new Intent(currectActivity.this, Algebra.class);
这篇关于ClassCastException异常:不能转换为android.app.Activity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文