我在片段中有一个列表,当我按下该列表中的某些内容时,我希望启动一个新活动。我只尝试了我的第一个选项,但是当我尝试它时,我得到了错误:
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();
}
}
}
我做错了什么?
答案 0 :(得分:2)
确保代数正在扩展活动,您也可以简单地发起这样的活动new Intent(currectActivity.this, Algebra.class);
答案 1 :(得分:0)
我从未使用过SherlockListFragment,但我使用了Fragment。当我想初始化一个新片段时,我使用了
NewFragmentClass newFragment = new NewFragmentClass ();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.myCurrentFragmentId, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();
我希望这个帮助