2013-03-20 135 views
1

我有一个片段中的列表,我想要一个新的活动开始,当我按下列表中的东西。我只试着用我的第一选择,但是当我尝试它,我得到的错误: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(); 
    } 
} 

}

我在做什么错?

+0

为什么你想启动这样一个新的活动?使用开关,只需在您的意图中更改班级名称并启动它。不需要使用字符串数组,并做类似的东西 – hardartcore 2013-03-20 15:32:55

+0

显示你的代码'com.test.test.Algebra'请 – Terry 2013-03-20 15:35:27

+0

看起来代数不是一个活动...确保你的代数类扩展Activity ..也发表代数类 – Pragnani 2013-03-20 15:36:57

回答

2

确保代数是伸出的活动,你也可以简单的发起这样new Intent(currectActivity.this, Algebra.class);

+0

当我尝试我得到 构造函数Intent(FragmentMatte,类)未定义 – GooGleye 2013-03-20 15:46:18

+0

此外因为它是这样的,它启动了任何lististem被点击的活动。 – GooGleye 2013-03-20 15:50:04

0

我从来没有使用过SherlockListFragment的活动,但我用了一个片段。当我想初始化一个新的片段,我用

NewFragmentClass newFragment = new NewFragmentClass(); 
FragmentTransaction transaction = getFragmentManager().beginTransaction(); 
transaction.replace(R.id.myCurrentFragmentId, newFragment); 
transaction.addToBackStack(null); 
// Commit the transaction 
transaction.commit(); 

我希望这有助于

+0

我要从一个片段到一个活动,而不是另一个片段:) – GooGleye 2013-03-20 15:48:04

相关问题