Android getIntExtra 总是返回默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2137514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:41:58  来源:igfitidea点击:

getIntExtra always returns the default value

android

提问by oregonduckman

I am trying to pass an integer between activities using an intent. The source activity makes the calls info.id is the selected item from a ListView.

我正在尝试使用意图在活动之间传递一个整数。源活动调用 info.id 是从 ListView 中选择的项目。

Intent intent = new Intent(myActivity.this, newClass.class); 
intent.putExtra("selectedItem", info.id); 
this.startActivity(intent); 

The target activity retrieves the intent using getIntent then calls

目标活动使用 getIntent 检索意图,然后调用

int iSelectedItem = intent.getIntExtra("selectedItem", -1); 

iSelectedItem is always -1 instead of the value passed to putExtra. Can someone tell me what I am doing wrong or do I misunderstand the use of intents?

iSelectedItem 始终为 -1,而不是传递给 putExtra 的值。有人可以告诉我我做错了什么还是我误解了意图的使用?

回答by Grant

The problem is that info.id will be a 'long' and is not converting to an 'int'. Try

问题是 info.id 将是一个“long”并且不会转换为“int”。尝试

long iSelectedItem = intent.getLongExtra("selectedItem", -1)

回答by Vikas

I don't find putIntExtra()method. So I ended up with following:

我没有找到putIntExtra()方法。所以我最终得到了以下结果:

intent.putExtra("jobId", 1);

and

Integer.parseInt(getIntent().getExtras().get("jobId").ToString());

Use try and catch to handle Exceptions.

使用 try 和 catch 来处理异常。

UPDATE

更新

Later I found that I was passing jobId as a string in putExtra()method, therefore getIntExtra()was always returning the default value.

后来我发现我在putExtra()方法中将 jobId 作为字符串传递,因此getIntExtra()总是返回默认值。

So @Grant is correct. You mustpass an Integer value in putExtra()method to use getIntExtra()method.

所以@Grant 是正确的。您必须putExtra()方法中传递一个整数值才能使用getIntExtra()方法。

回答by Dayvid Oliveira

I had this problem and it was a simple thing.

我遇到了这个问题,这是一件很简单的事情。

Check if you're using onActivityResult ... than, you don't have to use getIntent() to get the extras, you have to use the intent you pass as parameter.

检查您是否正在使用 onActivityResult ... 比,您不必使用 getIntent() 来获取额外内容,您必须使用您作为参数传递的意图。

In your case should be something like this:

在你的情况下应该是这样的:

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (resultCode == RESULT_OK) {
        int iSelectedItem = intent.getIntExtra("selectedItem", -1);             
        Log.d("DEBUG", "check iSelectedItem = " + iSelectedItem);
    }
}

observe that I'm not using getIntent(), but the argument intent.

请注意,我没有使用 getIntent(),而是使用参数意图。

(PS: if you're calling a lot of activities expecting results, is better to check if the intent is != null)

(PS:如果你调用了很多期待结果的活动,最好检查一下意图是否为 != null)

I hope it helps.

我希望它有帮助。

回答by Leonardo Sapuy

Easy:

简单:

Bundle bundle = getIntent().getExtras();
int iSelectedItem = bundle.getInt("selectedItem", -1);   

Now, if you're using StartActivityForResult and you want to return some data, from the child activity, remind that you have to use onActivityResult

现在,如果您正在使用 StartActivityForResult 并且想要从子活动返回一些数据,请提醒您必须使用 onActivityResult

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (resultCode == RESULT_OK) {
    int iSelectedItem = intent.getExtras.getInt("selectedItem", -1);             
    Log.d("DEBUG", "check iSelectedItem = " + iSelectedItem);
  }
}

Remember, is the same way like you get extras from other Activity, just using bundle.getInt, in this example, getExtras return a bundle, so, across this bundle, you can get any data that you have sent from the resultIntent.

请记住,与从其他 Activity 获取额外数据的方式相同,只是使用 bundle.getInt,在此示例中,getExtras 返回一个包,因此,通过此包,您可以获取从 resultIntent 发送的任何数据。

回答by grandia

In my case, it was because I created the object with mId member variable declared as string

就我而言,这是因为我创建了对象,其中 mId 成员变量声明为字符串

public class Application {

private String mId;
....
}

intent.putExtra("id", myApplication.getId());

and as such, the Extra is passed as string. simply change your member variable to int, you get the idea ;)

因此,Extra 以字符串形式传递。只需将您的成员变量更改为 int,您就明白了;)

回答by Ahamadullah Saikat

    int sub_menu_id = 0;
    int question_part = 0;

    if (savedInstanceState == null) {
        Bundle extras = getIntent().getExtras();
        if (extras == null) {
            sub_menu_id = -1;
            question_part = -1;
        } else {
            sub_menu_id = extras.getInt("sub_menu_id");
            question_part = extras.getInt("question_part");
        }
    }

    Log.d("DREG", "sub_menu_id: " + sub_menu_id + "\nquestion_part: " + question_part);