1

我在片段中有一个列表,当我按下该列表中的某些内容时,我希望启动一个新活动。我只尝试了我的第一个选项,但是当我尝试它时,我得到了错误:

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

}

我究竟做错了什么?

4

2 回答 2

2

确保代数正在扩展一项活动,您也可以像这样简单地启动一项活动new Intent(currectActivity.this, Algebra.class);

于 2013-03-20T15:35:18.220 回答
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();

我希望这有帮助

于 2013-03-20T15:43:55.253 回答