Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将Bundle传递到异步任务中_Android - Fatal编程技术网

Android 将Bundle传递到异步任务中

Android 将Bundle传递到异步任务中,android,Android,我正在一个异步任务中与bundle进行斗争。我有两个字符串要传递给AsyncTask,我想使用bundle来完成这个任务 main活动中的代码: Bundle adresses = new Bundle(); adresses.putString("to", textField1.getText().toString()); adresses.putString("from", textField2.getText().toString()); new PriceTask(getApplica

我正在一个异步任务中与bundle进行斗争。我有两个字符串要传递给AsyncTask,我想使用bundle来完成这个任务

main活动中的代码:

Bundle adresses = new Bundle();
adresses.putString("to", textField1.getText().toString());
adresses.putString("from", textField2.getText().toString());

new PriceTask(getApplicationContext()).execute(adresses); 
在我的任务中,我是这样做的:

protected Integer doInBackground(Bundle... b) {

   Bundle result = b[0];
   String to = result.getString("to");
   String from = result.getString("from");

}
值得一提的是,我的两个字符串包含如下内容

"Sometext here, and sometext here 1234"
如果我无法检索文本,我的调试器会说包包含正确的信息,但我的字符串将不包含正确的信息。当我调试并在字符串所在的位置设置断点时,它将只有以下值:

[t, o]

我做错了什么?提前感谢。

您做错的是在
doInBackground
方法中检索数据的方式。方法具有的参数是数组类型。请关注
..
Bundle result=b[0]中您在做什么
仅获取该数组的第0个元素,并将其传递给
Bundle
引用

您给出的代码和详细信息不足以给出完美的答案。如果所有给定代码都在同一个Java类中,则无需使用捆绑包。相反,您可以创建一个String类型的ArrayList,以包含从TextFields获取的值。然后doInBackground还包含一个ArrayList作为方法参数。然后获取所有列表项,并将“to”和“from”值分开


如果您坚持使用现有代码,请首先尝试找出
结果
变量中的内容。

在主活动中替换以下行:

Bundle adresses = new Bundle();
adresses.putString("to", textField1.getText().toString());
adresses.putString("from", textField2.getText().toString());

new PriceTask(getApplicationContext()).execute(adresses); 

在您的AsycTask中添加如下构造函数:

串到; 串从; 语境

公共YourAsyncTask(上下文上下文、字符串到、字符串从){ //TODO自动生成的构造函数存根 此.\u活动=\u活动; 这个; this.from=from;
}

您能发布您的Aync TAK的全部代码吗
new PriceTask(getApplicationContext(),textField1.getText().toString(),textField2.getText().toString()).execute();