J'essaie de passer une valeur int avec une intention à une autre activité, mais je reçois toujours 0. Et ce n'est pas 0, j'ai vérifié. J'essaie de passer la valeur de la variable int brojPoena. J'ai essayé ceci:Comment passer une variable int à une autre activité?
Intent i = new Intent(Game.this, Popup_opis.class);
i.putExtra("brojPoenaPrimljeno", brojPoena);
et dans mon activité de réception:
Intent mIntent = getIntent();
if(mIntent !=null) {
int brojPoena = mIntent.getIntExtra("brojPoenaPrimljeno", 0);
}
tvBrojPoena.setText("You won " + brojPoenaPrimljeno + " points");
Aussi j'essayé ceci:
Intent i = new Intent(getApplicationContext(), Popup_opis.class);
i.putExtra("brojPoenaPrimljeno", brojPoena);
et dans mon activité recieving:
Bundle extrasPoeni = getIntent().getExtras();
if(extrasPoeni !=null) {
brojPoenaPrimljeno = extras.getInt("brojPoena");
}
Mon activité de réception:
public class Popup_opis extends Activity implements OnClickListener{
TextView tvOpis,tvNaslov,tvBrojPoena;
String poslatOpis, primljenOpis;
int brojPoenaPrimljeno;
Button OK;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.popup_opis);
Bundle extras = getIntent().getExtras();
if(extras !=null) {
primljenOpis = extras.getString("poslatOpis");
}
Bundle extrasPoeni = getIntent().getExtras();
if(extrasPoeni !=null) {
brojPoenaPrimljeno = extras.getInt("brojPoena");
}
initVariables();
}
private void initVariables() {
Typeface tv = Typeface.createFromAsset(getAssets(), "ARIALN.TTF");
OK = (Button) findViewById(R.id.bOK);
tvOpis = (TextView) findViewById(R.id.tvOpis);
tvBrojPoena = (TextView) findViewById(R.id.tvBrojPoena);
tvBrojPoena.setTypeface(tv);
tvNaslov = (TextView) findViewById(R.id.tvNaslov);
tvNaslov.setTypeface(tv);
tvOpis.setTypeface(tv);
tvOpis.setText(primljenOpis);
tvBrojPoena.setText("Osvojili ste " + brojPoenaPrimljeno + " poena u ovoj igri.");
OK.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
Votre code est correct, mais comme le signale @codeMagic, vous ne transmettez probablement aucune valeur à l'activité de réception. – Darwind
Je fais, j'ai juste essayé de mettre cette valeur au bouton comme setText juste au-dessus de cette ligne d'intention et cela fonctionne. – marjanbaz