Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将计数器从一个活动传递到另一个活动_Android_Bundle_Android Activity - Fatal编程技术网

Android 将计数器从一个活动传递到另一个活动

Android 将计数器从一个活动传递到另一个活动,android,bundle,android-activity,Android,Bundle,Android Activity,我有一个类语音,它扩展了活动,并包含一个计数器。当用户正确回答时,计数器通过counter++添加一个 public class Voice extends Activity implements OnClickListener{ ListView lv; static final int check = 111; int counter_score; TextView txView; MediaPlayer ourSong; ImageView

我有一个类
语音
,它扩展了
活动
,并包含一个计数器。当用户正确回答时,计数器通过
counter++添加一个

public class Voice  extends Activity implements OnClickListener{
    ListView lv;
    static final int check = 111;
    int counter_score;
    TextView txView;
    MediaPlayer ourSong;
    ImageView display;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.letter_a);
        initialize();
    }

    private void initialize() {
        lv = (ListView)findViewById(R.id.lvVoiceReturn);
        Button b = (Button)findViewById(R.id.imageButtonSelector);
        txView = (TextView)findViewById(R.id.counter);
        b.setOnClickListener(this);
        counter_score=0;
    }
将此分数捆绑并传递到字符串“your score is 1”中的下一个活动“What”

…这是事情恶化的时候:(

What
上,我还有一个与计数器相关的问题。当用户正确回答时,计数器会通过
counter++;
添加一个计数器。但是,它会将
txview
字符串
更改为“您的分数为1”。我无法让它将1添加到字符串中上一个活动传递的计数器结果中,因此
What
上的计数器显示“您的分数为2”。这将传递到
Bundle keeper
中的下一个活动,该活动保存总分数


我读过一些关于传递
int
string
的教程,但是他们使用的一些代码,如
getInt
没有被识别。我被难住了。

你捆绑并传递给What activity的不是计数器,而是字符串“你的分数是1”。如果要在下一个活动中增加该数字,则应只发送整数值,并在其中构造所需的字符串

我读过一些关于传递int和字符串的tut,但是他们使用的一些代码,比如getInt,是不被识别的,不管是谁,我都被难住了

我不太确定我是否知道getInt()的含义不被识别。在任何情况下,在将计数器从一个活动传递到另一个活动时,请简化操作。如果它是一个int,并且您计划像接收活动中的int一样进行操作,则将其作为int添加到捆绑包中。例如:

    Bundle keeper = new Bundle();
    keeper.putInt("key", counter_score);
IntentToLaunchTheOtherActivity( counter_score );
并使用以下命令从捆绑包中检索它:

    Bundle gotKeeper = getIntent().getExtras();
    int score = gotKeeper.getInt("key");
如果让一个“全局”类在不同的活动之间共享,并使用它保持所使用的变量“同步”,该怎么办

例如-Globals.java:

public class Globals {

  public int counter_score; 

}
然后使用
Globals.counter\u score

public class What  extends Activity implements OnClickListener {
    ListView lv;
    static final int check = 111;
    private int counter_score;
    TextView txView;
    MediaPlayer ourSong;
    ImageView display;
    String gotScore;

    String classes[] = {"What", "Pagina", "What", "example3", "example4", "example5", 
                        "example6"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.letter_b);
        initialize();
        Bundle gotKeeper = getIntent().getExtras();
        gotScore = gotKeeper.getString("key");
        txView.setText(gotScore);
    }

    private void initialize() {
        // TODO Auto-generated method stub
        lv = (ListView)findViewById(R.id.lvVoiceReturn);
        Button b = (Button)findViewById(R.id.imageButtonSelector);
        txView = (TextView)findViewById(R.id.counter);
        b.setOnClickListener(this);
当然,您也可以将该共享类用于其他变量和函数,例如公共操作

更新 正如评论者所指出的,这种方法并不特别好——我忘记了代码只是被引用的,并且没有“活”起来为其他活动保存信息(感谢你在这一点上纠正我,我还在学习…)

不过,更好的方法是在启动第二个活动时,在INTERT中传递counter_score变量的当前状态,例如:

    Bundle keeper = new Bundle();
    keeper.putInt("key", counter_score);
IntentToLaunchTheOtherActivity( counter_score );

然后,如果变量在之后发生更改,可能会将其传递回上一个活动…

我让它工作了。基本上,我需要按照TJ Third的建议将
keeper.putString(“key”,counter_score);
转换为
keeper.putInt(“key”,counter_score)
,我还需要在“What”活动中将收到的bundle转换为int。在“What”活动中,我重命名了
int counter\u score;
int gotKeeper;
(这是String gotKeeper)然后,我没有调用counter_score=0;现在传递的bundle是int,而是在initialize()下调用counter_score=gotKeeper;因此计数器分数等于从上一个活动“Voice”生成的结果

现在,当用户回答正确时,counter++;将一个添加到现有的counter_分数中,并将其捆绑并发送到下一个活动,然后重复一次

static final int check = 111;
    int counter_score;
    TextView txView;
    MediaPlayer ourSong;
    ImageView display;
    int gotKeeper;


    String classes[] = {"What", "Pagina", "What", "example3", "example4", "example5", 
    "example6"};


@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.letter_b);
        initialize();
        Bundle gotKeeper = getIntent().getExtras();
        gotKeeper = gotScore.getInt("key");
        counter_score = gotKeeper;

再次感谢大家的建议和见解。对新手有很大的帮助。

除非所有变量都是静态的,否则这不起作用。这个解决方案仍然要求所有其他类都有一个Globals实例。所有这些实例都有自己的变量。此外,无法保证系统不会死机你的应用程序和获取活动之间的全局变量。这是一个罕见的事件,但可能会发生。如果你需要确保它永远不会发生,那么你会希望将计数器存储在某个文件中,或者在意图中来回传递。从int到string要容易得多,因此你希望存储整数。你可以扩展应用程序类并将您的全局变量存储在那里..嘿,伙计们,非常感谢大家的输入和建议。如果您的进度有更新,请编辑您的问题并在那里写入更新的信息。如果某个特定的答案,例如我的答案,有助于解决您的问题,请接受答案。