Android onActivityResult 中的数据为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10533280/
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:04:56  来源:igfitidea点击:

data in onActivityResult is null

androidandroid-intent

提问by Nobelisco

I am trying do a simple application for Android. I have two Activities (Aand B). In BI only want select a date.

我正在尝试为Android. 我有两个活动(AB)。在B我只想选择一个date.

I start A, and do:

我开始A,然后做:

 Intent intent = new Intent();
 intent.setClass(this, B.class);
 startActivityForResult(intent,1);

Then, in B, I do:

然后,在B,我做:

 Intent intent = getIntent();
 setResult(RESULT_OK);
 intent.putExtra("Date",dateSelected);
 finish();

And, in A, i have the next method:

而且,在A,我有下一个方法:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);
     if(resultCode==RESULT_OK && requestCode==1){
        Bundle bundle = getIntent().getExtras();
        String aux = bundle.getString("nuevo");
        .....
    }

But data, and bundle, are null. When i debug the code, i see that in class B, intenthas the Extras, but then, when i call finish()and return to class A, this intentis not reachable.

但是data, 和bundlenull。当我调试的代码,我看到,在课堂上BintentExtras,但后来,当我打电话finish(),并返回class A,这intent是不可达。

How can i solve this problem?

我怎么解决这个问题?

回答by ρяσ?ρ?я K

try this:

尝试这个:

Then, in B, I do:

然后,在 B 中,我这样做:

Intent intent = getIntent();
intent.putExtra("Date",dateSelected);
setResult(RESULT_OK, intent);
finish();

And, in A:

并且,在 A 中:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(resultCode==RESULT_OK && requestCode==1){
Bundle MBuddle = data.getExtras();
String MMessage = MBuddle .getString("Date");
}
}

回答by Samir Mangroliya

in AonActivityResult method

AonActivityResult 方法中

   if(null!=data){

    Bundle bundle = data.getExtras();
    String mydate = bundle.getString("Date");

   }

and in B

并在 B

Intent returnIntent = new Intent();
returnIntent.putExtra("Date",dateSelected);
setResult(RESULT_OK,returnIntent);      
finish();

回答by htafoya

I know this is answered, but just to give more explanation on the error, you were using getIntent()instead of the dataelement received on the callback.

我知道这是回答,但只是为了对错误提供更多解释,您使用getIntent()的是data在回调中接收到的元素。

getIntent()returns the Intent that was originally used to open Activity A(maybe when you opened the app or from another activity), and datais the intent that Activity Breturned as response parameters.

getIntent()返回最初用于打开Activity A的 Intent (可能是当您打开应用程序或从其他 Activity 时),并且dataActivity B作为响应参数返回的 Intent 。

Also, you were using getIntent()in Activity Binstead of creating a new Intent that would be returned to Activity A.

此外,您getIntent()Activity B中使用而不是创建一个将返回到 Activity A 的新 Intent。

Intent returnIntent = new Intent();

Finally, the created intent must be added in setResult

最后,必须将创建的意图添加到 setResult

setResult(RESULT_OK,returnIntent);