我正在尝试将多个整数变量值从第二个 Activity 返回到主要 Activity ,在第二个 Activity 中我有许多变量是通过不同的函数计算的,我需要返回到要添加到的主要 Activity 另一个变量的总和。
在我的主要 Activity 中,我使用 StartActivityForResult 开始了第二个 Activity
FindViewById<Button>(Resource.Id.btnAddItems).Click += delegate
{
var getMoreItems = new Intent (this, typeof(Activity2));
StartActivityForResult(getMoreItems,0);
};
在第二个 Activity 中,我尝试使用 PutExtra 和 Intent 返回值,我尝试返回 7 个整数变量和一个字符串。
FindViewById<Button>(Resource.Id.btnReturnItems).Click += delegate
{
Intent returnItems = new Intent(this, typeof(MainActivity));
string addOrSub = "add";
returnItems.PutExtra("putAddOrSub", addOrSub);
returnItems.PutExtra("putVar1", int1);
returnItems.PutExtra("putVar2", int2);
returnItems.PutExtra("putVar3", int3);
returnItems.PutExtra("putVar4", int4);
returnItems.PutExtra("putVar5", int5);
returnItems.PutExtra("putVar6", int6);
returnItems.PutExtra("putVar7", int7);
SetResult(Result.Ok, returnItems);
Finish();
};
我不完全确定,但我认为我的问题出在我的主要 Activity 的 OnActivityResult 重写中。首先我试过
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (resultCode == Result.Ok)
{
string addOrSub = data.GetStringExtra("putAddOrSub");
if (addOrSub == "add")
{
int tmpInt1 = data.GetIntExtra("putVar1");
int tmpInt2 = data.GetIntExtra("putVar2");
int tmpInt3 = data.GetIntExtra("putVar3");
int tmpInt4 = data.GetIntExtra("putVar4");
int tmpInt5 = data.GetIntExtra("putVar5");
int tmpInt6 = data.GetIntExtra("putVar6");
int tmpInt7 = data.GetIntExtra("putVar7");
updInt1 += tmpInt1;
updInt2 += tmpInt2;
updInt3 += tmpInt3;
updInt4 += tmpInt4;
updInt5 += tmpInt5;
updInt6 += tmpInt6;
updInt7 += tmpInt7;
anotherFunction();
}
}
}
我无法运行它,因为我收到了错误
“没有给定的参数对应于‘Intent.GetIntExtra(string, int)’的所需形式参数‘defaultValue’”
我决定尝试添加 Convert.ToInt32 并更改为 GetStringExtra 命令,因为我认为它出于某种原因将数据作为字符串返回。
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (resultCode == Result.Ok)
{
string addOrSub = data.GetStringExtra("putAddOrSub");
if (addOrSub == "add")
{
int tmpInt1 = Convert.ToInt32(data.GetStringExtra("putVar1"));
int tmpInt2 = Convert.ToInt32(data.GetStringExtra("putVar2"));
int tmpInt3 = Convert.ToInt32(data.GetStringExtra("putVar3"));
int tmpInt4 = Convert.ToInt32(data.GetStringExtra("putVar4"));
int tmpInt5 = Convert.ToInt32(data.GetStringExtra("putVar5"));
int tmpInt6 = Convert.ToInt32(data.GetStringExtra("putVar6"));
int tmpInt7 = Convert.ToInt32(data.GetStringExtra("putVar7"));
updInt1 += tmpInt1;
updInt2 += tmpInt2;
updInt3 += tmpInt3;
updInt4 += tmpInt4;
updInt5 += tmpInt5;
updInt6 += tmpInt6;
updInt7 += tmpInt7;
anotherFunction();
}
}
}
现在我可以在 Android Emulator 中运行它,但似乎没有返回任何值,我一直无法弄清楚原因。
请帮忙!! 请原谅我的笨拙,这是我第一次尝试使用 Android 应用程序,过去 5 个小时我一直在努力解决这个问题。
最佳答案
There is no argument given that corresponds to the required formal parameter 'defaultValue' of 'Intent.GetIntExtra(string, int)'
GetIntExtra
需要两个参数:
name String: The name of the desired item.
defaultValue int: the value to be returned if no value of the desired type is stored with the given name.
例子:如果putVar1
不存在,你想默认返回什么值:
int tmpInt1 = data.GetIntExtra("putVar1", 0);
文档:getIntExtra
关于c# - Android App - Xamarin - C# - 如何将多个整数变量值返回到主要 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49762054/