gpt4 book ai didi

java - 在两个 Activity 之间传递 int

转载 作者:行者123 更新时间:2023-11-30 09:22:13 24 4
gpt4 key购买 nike

我正在尝试在两个 Activity 之间传递 int 值。在第一个 Activity 中,我编写代码:

Class nextView = Class.forName("com.test.NextFile");                    
Intent nextIntentTest = new Intent(CurrentFile.this, NextFile.class);
nextIntentTest.putExtra("passingName",nameofvar);
startActivity(nextIntentTest);

然后,在第二个 Activity 中,

Intent intentTest = this.getIntent();
int counter = intentTest.getIntExtra("passingName",-1);

我不知道为什么,但我总是得到默认 (-1) 值。我对其他类和变量做了几乎相同的事情,一切都很好。也许问题出在 public class NextFile extends ListActivity 而不是 public class NextFile extends Avtivity

有人可以帮助我吗?

最佳答案

尝试

    Bundle c = new Bundle();

c.putInt("passingName",nameofvar);
yourintent.putExtras(c);

发布值(value)

    Bundle b = getIntent().getExtras();
int counter = b.getInt("passingName"); to retrieve it

关于java - 在两个 Activity 之间传递 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16609735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com