PutExtra在检索器端不起作用 [英] PutExtra doesn't work on the retriever side
问题描述
我想将意图从一个第一活动发送到另一活动.
I want to sent intent from one first activity to another.
第一个活动向第二个活动发送了一个意图,以便创建一个新的AlertDialog,从用户那里收到一个号码,然后将该号码发送回第一个活动,这是putExtra数据失败的地方.
The first activity sends an intent to the second activity in order to create a new AlertDialog, receive a number from the user and send the number back to the first activity, that's where putExtra data to failed.
第一个活动的代码:GuessItActivity.java
The code of the first activity: GuessItActivity.java
Intent intent = new Intent(GuessItActivity.this,
AlertDialogMessage.class);
intent.addFlags(Intent.FILL_IN_DATA);
intent.addFlags(Intent.FILL_IN_CATEGORIES);
intent.addFlags(Intent.FILL_IN_ACTION);
intent.putExtra("data1", 15);
startActivityForResult(
intent,
getResources().getInteger(
R.integer.ALERT_DIALOG_MESSAGE));
在检索器端,例如AlertDialogMessage.java的第二个活动端
on the retriever side , e.g the second activity side on AlertDialogMessage.java
public void onClick(DialogInterface dialog,
int which) {
// "OK" button pressed
int userGuess = Integer.parseInt(input
.getText().toString());
Intent intent = getIntent();
if (intent == null)
return;
int data1 = intent.getIntExtra("data1", -1);
if (data1 != 15 )
return; // data1 ==15
intent.putExtra("data2", 25);
if (getParent() == null) {
setResult(Activity.RESULT_OK, intent);
} else {
getParent().setResult(Activity.RESULT_OK,
intent);
}
finish();
第一个活动方面:回到GuessItActivity.java
The first activity side: back to GuessItActivity.java
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == getResources().getInteger(
R.integer.ALERT_DIALOG_MESSAGE)) {
if (resultCode == RESULT_OK) {
Intent intent = getIntent();
if (intent==null)
return;
Bundle extra = getIntent().getExtras();
if (extra != null) {
int _data1 = extra.getInt("data1");
int _data2 = extra.getInt("data2");
}
// extra == null , what am I doing wrong ?
int data1 = intent.getIntExtra("data1",-1);
int data2 = intent.getIntExtra("data2",-1);
if ((data1==-1)&&(data1==-1))
return;
问题是,我收到的data1和data2等于-1.
The problem is , that I receive data1 and data2 equal to -1.
我想接收我在第二个活动中输入的数据.例如data1 == 15和data2 == 25
I'd like to receive the data the I put on the second activity. e.g data1 == 15 and data2 == 25
我在做什么错了?
推荐答案
尝试
Bundle extra=data.getExtras();
装有
Bundle extra=getIntent().getExtras();
这篇关于PutExtra在检索器端不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!