0

我在 Activity 和 FragmentActivity 之间传递额外内容时遇到问题。

下一个代码创建 Intent 以启动新的 Activity:

Intent fichaSerie = new Intent(getActivity(),ActividadSerie.class);
NotificacionSerie serie = (NotificacionSerie) adaptador.getItem(position);
fichaSerie.putExtra("serie", serie.getID());
getActivity().startActivity(fichaSerie);

当我试图在 Fragment Activity 中获取 Extras 时,我得到一个 NullPointerException:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.serie);
String extra = getIntent().getExtras().getString("serie") //NullPointerException
}

有人知道我在代码中犯了什么错误吗?

非常感谢大家。

4

3 回答 3

3

谢谢你们所有的帮助,我用这个电话解决了我的问题:

getIntent().getStringExtra("serie")

我将此问题标记为已解决。

再次感谢你们。

于 2012-08-25T11:44:12.473 回答
1

首先,Java 区分大小写,因此.getID().getId(). 如果您尝试获取对象的 ID,请使用.getId(). 这通常会返回一个Integer

type正如 Michal 所说(字符串或整数或其他任何内容) ,请注意您正在投入的额外内容

Intent fichaSerie = new Intent(getApplicationContext(), ActividadSerie.class);
NotificacionSerie serie = (NotificacionSerie) adaptador.getItem(position);
fichaSerie.putExtra("serie", serie.getId()); //presuming this id is an Integer or long int ... You could change this to string if it is still usable then (serie.getId().toString())... but you have to get an intent with myIntent.getStringExtra(...), but I think you will not be doing this.
startActivityForResult(fichaSerie, 0);

从 ActividadSerie.java 获得额外收益

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.serie);
Intent myIntent = getIntent();
String extra = myIntent.getStringExtra("serie"); //try diferent gets... getIntExtra(), getStringExtra()..
}
于 2012-08-25T08:15:37.640 回答
0

您被输入一个Integer值并将其放入该String值中,以便它获得一个NullPointerException.

于 2012-08-25T09:57:45.977 回答