I got error on `onPreExecute()’
See my code :
I have confuse here(
download method (loadDailyDownloadData)contain in the different class & calling AsyncTask is different class…)
public class ListRoutesActivity extends Activity {
public void onNextAction(View view){
if(selectedRoutes.size() > 0){
StringBuffer routeCode = new StringBuffer();
for(int i=0; i<selectedRoutes.size();i++){
routeCode.append("\'" + selectedRoutes.get(i) +"\'" +",");
}
routeCode.delete(routeCode.length() - 1,routeCode.length());
strUField1 = routeCode.toString();
new DailyDownldAsyncTask().execute();
// String s = new DownlaodTableActivity().loadDailyDownloadData(strBusinessUnit, strExecutive,strTerritoryCode,strUField1);
// System.out.println(" ---s - " + s);
}else{
Toast.makeText(ListRoutesActivity.this,"Please select the Route!",Toast.LENGTH_SHORT).show();
}
return;
}
public class DailyDownldAsyncTask extends AsyncTask<String, Integer, String> {
private final ProgressDialog dialog = new ProgressDialog(ListRoutesActivity.this);
int myProgress;
@Override
protected void onPostExecute(String result) {
System.out.println(" ---- result--- " + result);
if (this.dialog.isShowing()) {
this.dialog.dismiss();
}
Toast.makeText(ListRoutesActivity.this,"Download successfully",Toast.LENGTH_SHORT).show();
}
@Override
protected void onPreExecute() {
this.dialog.setMessage("Downloading Table Data.......");
this.dialog.show();
myProgress = 0;
}
@Override
protected String doInBackground(String... params) {
return new DownlaodTableActivity().loadDailyDownloadData(strBusinessUnit, strExecutive,strTerritoryCode,strUField1);
}
@Override
protected void onProgressUpdate(Integer... values) {
dialog.setProgress(values[0]);
}
}}
Here is my logcat :
08-19 17:45:18.933: ERROR/AndroidRuntime(472): FATAL EXCEPTION: main
08-19 17:45:18.933: ERROR/AndroidRuntime(472): java.lang.IllegalStateException: Could not execute method of the activity
08-19 17:45:18.933: ERROR/AndroidRuntime(472): at android.view.View$1.onClick(View.java:2144)
08-19 17:45:18.933: ERROR/AndroidRuntime(472): at android.view.View.performClick(View.java:2485)
08-19 17:45:18.933: ERROR/AndroidRuntime(472): at android.view.View$PerformClick.run(View.java:9080)
08-19 17:45:18.933: ERROR/AndroidRuntime(472): at android.os.Handler.handleCallback(Handler.java:587)
08-19 17:45:18.933: ERROR/AndroidRuntime(472): at android.os.Handler.dispatchMessage(Handler.java:92)
08-19 17:45:18.933: ERROR/AndroidRuntime(472): at android.os.Looper.loop(Looper.java:123)
08-19 17:45:18.933: ERROR/AndroidRuntime(472): at android.app.ActivityThread.main(ActivityThread.java:3683)
08-19 17:45:18.933: ERROR/AndroidRuntime(472): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 17:45:18.933: ERROR/AndroidRuntime(472): at java.lang.reflect.Method.invoke(Method.java:507)
08-19 17:45:18.933: ERROR/AndroidRuntime(472): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-19 17:45:18.933: ERROR/AndroidRuntime(472): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-19 17:45:18.933: ERROR/AndroidRuntime(472): at dalvik.system.NativeStart.main(Native Method)
08-19 17:45:18.933: ERROR/AndroidRuntime(472): Caused by: java.lang.reflect.InvocationTargetException
08-19 17:45:18.933: ERROR/AndroidRuntime(472): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 17:45:18.933: ERROR/AndroidRuntime(472): at java.lang.reflect.Method.invoke(Method.java:507)
08-19 17:45:18.933: ERROR/AndroidRuntime(472): at android.view.View$1.onClick(View.java:2139)
08-19 17:45:18.933: ERROR/AndroidRuntime(472): ... 11 more
08-19 17:45:18.933: ERROR/AndroidRuntime(472): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@4054c798 is not valid; is your activity running?
08-19 17:45:18.933: ERROR/AndroidRuntime(472): at android.view.ViewRoot.setView(ViewRoot.java:527)
08-19 17:45:18.933: ERROR/AndroidRuntime(472): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
08-19 17:45:18.933: ERROR/AndroidRuntime(472): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-19 17:45:18.933: ERROR/AndroidRuntime(472): at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-19 17:45:18.933: ERROR/AndroidRuntime(472): at android.app.Dialog.show(Dialog.java:241)
08-19 17:45:18.933: ERROR/AndroidRuntime(472): at com.xont.controller.admin.ListRoutesActivity$DailyDownldAsyncTask.onPreExecute(ListRoutesActivity.java:164)
08-19 17:45:18.933: ERROR/AndroidRuntime(472): at android.os.AsyncTask.execute(AsyncTask.java:391)
08-19 17:45:18.933: ERROR/AndroidRuntime(472): at com.xont.controller.admin.ListRoutesActivity.onNextAction(ListRoutesActivity.java:100)
08-19 17:45:18.933: ERROR/AndroidRuntime(472): ... 14 more
Without AsynTask my functionality is working fine that mean if I call new DownlaodTableActivity().loadDailyDownloadData(strBusinessUnit, strExecutive,strTerritoryCode,strUField1); this method directly without AsynTask , Its working fine
Please Help me
Thanks in advance
As it seems, your
DownlaodTableActivity()is indeed not running. You can’t start an activity but just instantiating it, you need to callstartActivity(Intent), passing theIntentfor the activity you want to start.However, your code looks weird. The
DownlaodTableActivity().loadDailyDownloadData(strBusinessUnit, strExecutive,strTerritoryCode,strUField1);method can be placed in a class that doesn’t extend activity, then you’ll have no problems. Hope this helps.