我试图将一个int值传递给另一个 Activity ,但我总是得到0。我检查了一下,它不是0。我正在尝试从 int 变量 brojPoena 传递值。我试过这个:
Intent i = new Intent(Game.this, Popup_opis.class);
i.putExtra("brojPoenaPrimljeno", brojPoena);
在我的接收 Activity 中:
Intent mIntent = getIntent();
if(mIntent !=null) {
int brojPoena = mIntent.getIntExtra("brojPoenaPrimljeno", 0);
}
tvBrojPoena.setText("You won " + brojPoenaPrimljeno + " points");
我也尝试过这个:
Intent i = new Intent(getApplicationContext(), Popup_opis.class);
i.putExtra("brojPoenaPrimljeno", brojPoena);
在我的接收 Activity 中:
Bundle extrasPoeni = getIntent().getExtras();
if(extrasPoeni !=null) {
brojPoenaPrimljeno = extras.getInt("brojPoena");
}
我的接收 Activity :
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
}
}
最佳答案
这个变量brojPoenaPrimljeno
在哪里?你有这个
int brojPoena = mIntent.getIntExtra("brojPoenaPrimljeno", 0);
但在调用setText()
时使用不同的变量名称
您正在尝试使用值
而不是键
接收值。当您创建Intent
i.putExtra("brojPoenaPrimljeno", brojPoena); // brojPoenaPrimljeno is the key be trying to use to
尝试
brojPoenaPrimljeno = getIntent().getIntExtra("brojPoenaPrimljeno", 0);
此外,这是一个小问题,不是您的问题,但效率低下,可能会导致问题。您在两个不同的地方得到了“Intent ”。这里
Bundle extras = getIntent().getExtras();
这里
Bundle extrasPoeni = getIntent().getExtras();
处理来自不同Activity
的 Intent
如果这有帮助,如果我有一个Activity
从多个地方接收Intents
,我将使用String extra
来告诉接收者它来自的Activity
。例如:
Intent Intent = new Intent(SendingActivity.this, ReceivingActivity.class); Intent.putExtra("源", "Activity 名称");//这将用于了解 Intent 来自何处
//receiving activity
Intent recIntent = getIntent();
if (recIntent.getStringExtra("source") != null)
{
String source = recIntent.getStringExtra("source");
if (source.equals("activityName"))
{
// do stuff
}
if (source.equals("differentActivityName"))
{
// do other stuff
}
}
关于java - 如何将 int 变量传递给另一个 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15799637/