如何将Asynctask(不同类)中的值返回到您调用Asynctask的活动,这里我遵循以下链接中给出的intsruction 如何将OnPostExecute()的结果导入主活动,因为AsyncTask是一个单独的类? @ HelmiB's
我已经做了所有事情,并将结果返回到activity的方法processFinish(),问题是失去了活动控制或焦点,我无法使用Asynctask的结果做进一步的操作,因为我的所有活动的成员都变为null。
如何进行 ?
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; }`How to return a value from Asynctask (Different class) to activity back from which you called the Asynctask , here i have followed the intsruction given in following link How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class? @HelmiB's
I have done everything and its returning the result to activity's method processFinish() also , the problem is lost the activity control or focus, i could not do further actions using the Asynctask's result, as because all my activity's members becomes null.
How to proceed ?
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; }`最满意答案
好吧,你的问题出在这行代码中:
asyncCallWS = new AsyncCallWS(MainActivity.this, input, position,new MainActivity());或者,特别是在语句new MainActivity() 。 您可以在此处创建MainActivity类的新实例,然后将其用作回调。 显然,它将使所有字段都未初始化。 使用MainActivity.this而不是new MainActivity() 。 请记住,Android管理您的所有活动。 你永远不想自己创造一个。
Well, your problem is in this line of code:
asyncCallWS = new AsyncCallWS(MainActivity.this, input, position,new MainActivity());Or, particularly, in the statement new MainActivity(). You create a new instance of MainActivity class here and then you use it as a callback. Obviously, it will have all the fields non-initialized. Use MainActivity.this instead of new MainActivity(). And please remember that Android manages all of your activities. You never want to create one yourself.
如何将值从Asynctask返回到活动(How to return a value from Asynctask to activity)如何将Asynctask(不同类)中的值返回到您调用Asynctask的活动,这里我遵循以下链接中给出的intsruction 如何将OnPostExecute()的结果导入主活动,因为AsyncTask是一个单独的类? @ HelmiB's
我已经做了所有事情,并将结果返回到activity的方法processFinish(),问题是失去了活动控制或焦点,我无法使用Asynctask的结果做进一步的操作,因为我的所有活动的成员都变为null。
如何进行 ?
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; }`How to return a value from Asynctask (Different class) to activity back from which you called the Asynctask , here i have followed the intsruction given in following link How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class? @HelmiB's
I have done everything and its returning the result to activity's method processFinish() also , the problem is lost the activity control or focus, i could not do further actions using the Asynctask's result, as because all my activity's members becomes null.
How to proceed ?
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; }`最满意答案
好吧,你的问题出在这行代码中:
asyncCallWS = new AsyncCallWS(MainActivity.this, input, position,new MainActivity());或者,特别是在语句new MainActivity() 。 您可以在此处创建MainActivity类的新实例,然后将其用作回调。 显然,它将使所有字段都未初始化。 使用MainActivity.this而不是new MainActivity() 。 请记住,Android管理您的所有活动。 你永远不想自己创造一个。
Well, your problem is in this line of code:
asyncCallWS = new AsyncCallWS(MainActivity.this, input, position,new MainActivity());Or, particularly, in the statement new MainActivity(). You create a new instance of MainActivity class here and then you use it as a callback. Obviously, it will have all the fields non-initialized. Use MainActivity.this instead of new MainActivity(). And please remember that Android manages all of your activities. You never want to create one yourself.
发布评论