android - ClassCastException starting AsyncTask in service -


i'm getting error when starting service starts asynctask - , yet have similar service , asynctask work ok. both services declared in manifest. if can suggest might problem i'd appreciate it. after lots of reading i've assumed it's structure/parameters of asynctask, i've tried few variations no joy yet - i'm not sure i'm going in right direction. i'm not explicitly extending application anywhere.

09-04 09:13:46.444: e/androidruntime(1190): fatal exception: main 09-04 09:13:46.444: e/androidruntime(1190): java.lang.runtimeexception: unable start service couk.jit.currencycheck1.serviceclassxml@4602fc20 intent { flg=0x4 cmp=couk.jit.currencycheck1/.serviceclassxml (has extras) }: java.lang.classcastexception: android.app.application 09-04 09:13:46.444: e/androidruntime(1190):     @ android.app.activitythread.handleserviceargs(activitythread.java:3063) 09-04 09:13:46.444: e/androidruntime(1190):     @ android.app.activitythread.access$3600(activitythread.java:125) 09-04 09:13:46.444: e/androidruntime(1190):     @ android.app.activitythread$h.handlemessage(activitythread.java:2096) 09-04 09:13:46.444: e/androidruntime(1190):     @ android.os.handler.dispatchmessage(handler.java:99) 09-04 09:13:46.444: e/androidruntime(1190):     @ android.os.looper.loop(looper.java:123) 09-04 09:13:46.444: e/androidruntime(1190):     @ android.app.activitythread.main(activitythread.java:4627) 09-04 09:13:46.444: e/androidruntime(1190):     @ java.lang.reflect.method.invokenative(native method) 09-04 09:13:46.444: e/androidruntime(1190):     @ java.lang.reflect.method.invoke(method.java:521) 09-04 09:13:46.444: e/androidruntime(1190):     @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:868) 09-04 09:13:46.444: e/androidruntime(1190):     @ com.android.internal.os.zygoteinit.main(zygoteinit.java:626) 09-04 09:13:46.444: e/androidruntime(1190):     @ dalvik.system.nativestart.main(native method) 09-04 09:13:46.444: e/androidruntime(1190): caused by: java.lang.classcastexception: android.app.application 09-04 09:13:46.444: e/androidruntime(1190):     @ couk.jit.currencycheck1.getxmltask.<init>(getxmltask.java:52) 09-04 09:13:46.444: e/androidruntime(1190):     @ couk.jit.currencycheck1.serviceclassxml.onstartcommand(serviceclassxml.java:24) 09-04 09:13:46.444: e/androidruntime(1190):     @ android.app.activitythread.handleserviceargs(activitythread.java:3053) 09-04 09:13:46.444: e/androidruntime(1190):     ... 10 more 

this service works running asynctask below it.

public class serviceclass extends service { sharedpreferences prefs = null;  @override public int onstartcommand(intent intent, int flags, int startid) {     prefs = preferencemanager.getdefaultsharedpreferences(this.getapplication().getapplicationcontext());     boolean run = prefs.getboolean("running", false);     if (!run)         stopself();     else {         new asyncnetworkconnection(this.getapplication().getapplicationcontext(), true).execute("");     }     return super.onstartcommand(intent, flags, startid); }  @override public ibinder onbind(intent arg0) {     // todo auto-generated method stub     return null; } } 

asynctask works.

public class asyncnetworkconnection extends asynctask<string, string, string> {  public asyncnetworkconnection(context ct, boolean asservice) { ... }  @override protected void onpostexecute(string result) { ... }  protected string doinbackground(string... arg0) { string result = null; ... results = fetchhtml(arg0[0]); return results; }  private string fetchhtml(string urlstr) throws urisyntaxexception,         clientprotocolexception, ioexception, exception { string result = null; ... return result; }  } 

this service giving error. if comment out 'newgetxmltask' line, don't error. if replace other asynctask don't error (but of course subsequent errors). log message shown in logcat.

public class serviceclassxml extends service {  @override public int onstartcommand(intent intent, int flags, int startid) {     log.i("dbg", "serviceclassxml started");     prefs = preferencemanager.getdefaultsharedpreferences(this.getapplication().getapplicationcontext());     boolean run = prefs.getboolean("running", false);     if (!run)         stopself();     else {         new getxmltask(this.getapplication().getapplicationcontext(), true).execute("abc"); //          new asyncnetworkconnection(this.getapplication().getapplicationcontext(), true).execute("");     }     return super.onstartcommand(intent, flags, startid); }  @override public ibinder onbind(intent arg0) {     // todo auto-generated method stub     return null; } } 

the basics of asynctask gives error.

public class getxmltask extends asynctask<string, void, list<ratedata>> {  public getxmltask(context ct, boolean asservice) {     this.context = (activity) ct;   //as context can cast activity     prefs = preferencemanager.getdefaultsharedpreferences(context);     datasource = prefs.getint("srcs", 0);     runasservice = asservice;     restoredtextfrom = prefs.getstring("fromcurr", null);     restoredtextto = prefs.getstring("tocurr", null); }  @override protected void onpostexecute(list<ratedata> rates) { ... }  @override protected list<ratedata> doinbackground(string... urls) { xml = getxmlfromurl(url); ... }  private string getxmlfromurl(string urlstring) { ... return output.tostring(); } } 

manifest includes:

<application     android:allowbackup="true"     android:icon="@drawable/ic_launcher"     android:label="@string/app_name"     android:theme="@style/apptheme" >     <activity         android:name="couk.jit.currencycheck1.mainmenu"         android:label="@string/app_name" >         <intent-filter>             <action android:name="android.intent.action.main" />              <category android:name="android.intent.category.launcher" />         </intent-filter>     </activity>     <service         android:name="couk.jit.currencycheck1.serviceclass"         android:enabled="true" />     <service         android:name="couk.jit.currencycheck1.serviceclassxml"         android:enabled="true" /> ... </application> 

you should make varible context.

 context ctx = getapplicationcontext(); 

and use ctx in

 new getxmltask(ctx, true).execute("abc"); 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -