Android-切换活动

Android-切换活动,android,android-activity,Android,Android Activity,我正在开发一个使用ListActivity的android应用程序 Intent i = getIntent(); obj = i.getExtra("somekey"); // this will depend on the type of Extra. 在onListItemClick方法中,我实例化了一个对象x。我有一个活动a,其构造函数接收和对象的类型相同的x。我如何实例化一个实例并启动它 很像这样,但不起作用: protected void onListItemClick(ListV

我正在开发一个使用ListActivity的android应用程序

Intent i = getIntent();
obj = i.getExtra("somekey"); // this will depend on the type of Extra.
在onListItemClick方法中,我实例化了一个对象x。我有一个活动a,其构造函数接收和对象的类型相同的x。我如何实例化一个实例并启动它

很像这样,但不起作用:

protected void onListItemClick(ListView l, View v, int position, long id) {
    EventoSingle eventoSingle = new EventoSingle(this.eventos.get(position));
    Intent i = new Intent(this, EventoSingle.class);
    eventoSingle.startActivity(i);
    startActivity(i);
    super.onListItemClick(l, v, position, id);
}

你不能那样做

不,你做得不对

你需要这样做

Intent i = new Intent(this, EvenToSingle.class);
i.putExtra("somekey", this.eventos.get(position)); // this will depend on the type of extra
startActivity(i);
然后在onCreate中创建新活动

Intent i = getIntent();
obj = i.getExtra("somekey"); // this will depend on the type of Extra.

用人们在答案中告诉我的方法解决了这个问题。 但随后发生了另一个错误:

newInstance失败:否


然后我检查了一下,一切正常。

当我这样做时,我得到了一个异常:java.lang.RuntimeException:无法实例化活动组件信息{org.android.catsMobile/org.android.catsMobile.EventoSingle}:java.lang.InstantiationException:org.android.catsMobile.EventoSingle