【发布时间】:2015-07-23 00:44:13
【问题描述】:
如何将值从 Asynctask(不同的类)返回到您调用 Asynctask 的活动,这里我遵循以下链接中给出的说明How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?@HelmiB's
我已经完成了所有操作,并将结果返回给活动的方法 processFinish(),问题是失去了活动控制或焦点,我无法使用 Asynctask 的结果执行进一步的操作,因为我的所有活动的成员都变为空。
如何进行?
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog != null)
{
pDialog.cancel();
}
if (StringUtil.hasValue(responseXmlString))
{
if (Integer.parseInt(AppUtil.getXpathValue("Result/ErrorNo",AppUtil.buildDocument(responseXmlString))) == 0)
{
asyncResponse.processFinish(responseXmlString);
}
}
@Override
public void processFinish(Object output)
{
Log.d("Response From Asynchronous task:", (String) output);
displayView(position,(String) output);
}
private void displayView(int position,String responseXml)
{
// update the main content by replacing fragments
boolean isFragment = true;
Fragment fragment = null;
/*//For Handling back press
if (getIntent().getBooleanExtra("FromPassBook", false) )
{
getIntent().putExtra("FromPassBook", false);
position = 7;
}
if (getIntent().getBooleanExtra("toCustomerAcocunts", false) )
{
getIntent().putExtra("toCustomerAcocunts", false);
position = 1;
}*/
switch (position)
{
case 0:
fragment = new ChartFragment();
setTitle(getResources().getString(R.string.wealth));
break;
default:
break;
}
if (fragment != null && isFragment)
{
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction ft =fragmentManager.beginTransaction();
fragmentStack.push(fragment);
//passing data to fragment
if (StringUtil.hasValue(responseXml))
{
Bundle bundle = new Bundle();
bundle.putString("responseXml", responseXml);
fragment.setArguments(bundle);
}
ft.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
listView.setItemChecked(position, true);
listView.setSelection(position);
mDrawerLayout.closeDrawer(listView);
}
else
{
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
private void callService(int position)
{
String input = "";
AsyncCallWS asyncCallWS;
switch (position)
{
case 0:
input = "<Parm><ProcessID>101</ProcessID><MobileNo>" + mobileNumber + "</MobileNo></Parm>";
asyncCallWS = new AsyncCallWS(MainActivity.this, input, position,new MainActivity());
asyncCallWS.execute();
break;
}
Asynctask class constructor
`AsyncResponse asyncResponse;
public AsyncCallWS(Context context,String input,int position,AsyncResponse response )
{
this.context = context;
this.inputToservice = input;
this.position = position;
asyncResponse = response;
}`
【问题讨论】:
-
请贴一些代码
-
如果您想在活动丢失的情况下进行处理,您应该使用 RoboSpice 或 Datadroid(已弃用)之类的东西
-
您的问题中似乎缺少一些上下文。为什么所有活动成员都变为空?您是否通过旋转设备重新创建了活动?
-
提供一些关于您的活动、侦听器和 Asynctask 类的代码。
-
那么哪些变量是空的,同时设备会发生什么?