Android ListItem不起作用

时间:2012-08-02 13:59:56

标签: java android

当我点击列表项时,它不会启动新活动。是什么原因?其他人已经实现了相同的代码,并且程序正在运行。我该怎么办这个问题? 这是代码:

public class Menu extends ListActivity {

    String classes[]={"StartingPoint","Splash", "You", "Dumb"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, classes));
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        String cheese = classes[position];
        try {
            Class ourclass = Class.forName("com.umer.splashscreenwithsound." + cheese);
            //Class ourclass = Class.forName("com.umer.splashscreenwithsound.StartingPoint");
            Intent myintent = new Intent(Menu.this,ourclass);
            startActivity(myintent);
        } catch(ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

请帮忙。

1 个答案:

答案 0 :(得分:1)

您需要在onCreate()中设置点击的监听器

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, classes));
        ListView lv = getListView();
        lv.setOnItemClickListener(this);
    }