无法getIntExtra()时的onActivityResult [英] Can not getIntExtra() at onActivityResult

查看:715
本文介绍了无法getIntExtra()时的onActivityResult的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题通过意图从一个活动到另一个发送和接收int型;

I got a problem with sending and receiving int types from one activity to another via Intent;

我正在与的onActivityResult()函数被放置在接收活动发送。

I'm sending it with onActivityResult() function which is placed at the receiving activity.

的code:

发送活动:

The Sending Activity:

Intent ba=new Intent();

MyPoints = fgv.getPoints();

int MP=(int)MyPoints;
Log.i("Problem","MyPoints MP = "+MP);

ba.putExtra("FocusScore",MP);
Log.i("Problem","MyPoints = "+MP);

setResult(RESULT_OK,ba);
finish();

在接收活动:

//在onClick的以移动其他类

//At the onClick in order to move the the other class

意图goFocus =新意图(Games.this,FocusGame.class);

Intent goFocus =new Intent(Games.this,FocusGame.class);

startActivityForResult(goFocus,1);

startActivityForResult(goFocus,1);

//在函数的onActivityResult

//At the onActivityResult function

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode==RESULT_OK)
        {   

          switch (requestCode) {

             case 1: {
             //Coming back from Focus-Game
             //Problem:!!

              int sss= getIntent().getIntExtra("FocusScore", -1); 
              Log.i("Problem","sss = "+sss);

                 }
             break;

             default :
             break;

            }

}

在code的结果给出一个日志,其中SSS = -1。这意味着

The result of the code is given a log where sss=-1. Which means that

getIntent()getIntExtra();

getIntent().getIntExtra();

总是空。

而MP的登录工作正常。

And the Log of the MP is working fine.

希望你能帮助我在这里。

Hope you could help me over here.

-
提前致谢,
亚尼夫。

- Thanks in advance, Yaniv.

推荐答案

使用数据的价值不是 getIntent()

if (data != null) {
    //int sss= getIntent().getIntExtra("FocusScore", -1); 
    Bundle extras = data.getExtras();
    int sss =  extras.getInt("FocusScore");
}

或只是

if (data != null) {
    //int sss= getIntent().getIntExtra("FocusScore", -1);     
    int sss= data.getIntExtra("FocusScore", -1); 
}

更多信息: 无法从意图获取数据 - Android电子

More info: Cannot get Data from the intent - Android

这篇关于无法getIntExtra()时的onActivityResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆