0

Activity と FragmentActivity の間でエクストラを渡すのに問題があります。

この次のコードは、新しいアクティビティを開始するインテントを作成します。

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

フラグメント アクティビティでエクストラを取得しようとすると、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値を返します

typeMichalが言ったように、余分に入れていることに注意してください(文字列または整数またはその他のもの)

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 に答える