我有这段代码:
Rechercher.java:
public void doOnResult(String json){
if ( json.equals("Aucune propostion pour le mois")||json.equals("Aucune propostion pour cette date")) {
Toast.makeText(Rechercher.this, "Aucune proposition actuellement.", Toast.LENGTH_LONG).show();
finish();
} else {
Intent iAfficher = new Intent(this, Afficher.class);
extras.putString("json", json);
extras.putInt("nbplaces", mCounter);
iAfficher.putExtras(extras);
this.startActivityForResult(iAfficher, 10);
}
}
Afficher.java:
Integer places = extras.getInt("nbplaces");
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.afficher);
ListView lvTrajets = (ListView)findViewById(R.id.lvTrajets);
Bundle bundle = getIntent().getExtras();
.......
proposition.put("Date",propId);
proposition.put("Trajet", propLieu+" de "+propVille+" --> "+propGare+" Places : "+places);
places变量始终等于0.
我不知道为什么我无法获得正确的价值。
答案 0 :(得分:1)
将此Integer places = extras.getInt("nbplaces");
移到onCreate
内。还初始化extras
。
public Intent getIntent ()
Added in API level 1
Return the intent that started this activity.
而不是Integer places
使用int places
。 int
是原始数据类型,因此对原始数据类型使用int
而不是Integer
。
int places;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.afficher);
Bundle extras = getIntent().getExtras();
places = extras.getInt("nbplaces");
您需要等到活动创建后再使用getIntent()
http://developer.android.com/reference/android/os/Bundle.html#getInt(java.lang.String)
public int getInt (String key)
Added in API level 1
Returns the value associated with the given key, or 0 if no mapping of the desired type exists for the given key.
Parameters
key a String
Returns
an int value
答案 1 :(得分:1)
Integer places ;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.afficher);
Bundle bundle = getIntent().getExtras();
places = bundle.getInt("nbplaces")
...........
答案 2 :(得分:1)
在onCreate()方法
中的bundle声明之后放入以下行 Integer places = bundle.getInt("nbplaces");
并删除以下行
Integer places = extras.getInt("nbplaces");
答案 3 :(得分:1)
您需要通过Bundle
方法获取Afficher.java
文件中onCreate
的值,如下所示:
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.afficher);
ListView lvTrajets = (ListView)findViewById(R.id.lvTrajets);
Bundle bundle = getIntent().getExtras();
Integer places = bundle.getInt("nbplaces"); //get value here
String jsonval=bundle.getString("json");