如何将Asynctask(不同类)中的值返回到您调用Asynctask的活动,这里我已经按照以下链接How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class? @ HelmiB'中给出的intsruction进行了操作
我已经做了所有事情,并将结果返回到活动的方法processFinish(),问题是失去了活动控制或焦点,我无法使用Asynctask的结果做进一步的操作,如因为我所有活动的成员都变为空。
如何进行?
Directory.GetFiles()
`AsyncResponse asyncResponse;
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
答案 0 :(得分:0)
嗯,你的问题出在这行代码中:
asyncCallWS = new AsyncCallWS(MainActivity.this, input, position,new MainActivity());
或者,特别是在new MainActivity()
声明中。您在此处创建MainActivity
类的新实例,然后将其用作回调。显然,它将使所有字段都未初始化。使用MainActivity.this
代替new MainActivity()
。请记住Android管理您的所有活动。你永远不想自己创造一个。