启动启动 AsyncTask 的服务时出现此错误 - 但我有另一个类似的服务和 AsyncTask 可以正常工作。这两个服务都在清单中声明。如果有人可以提出可能是什么问题,我将不胜感激。经过大量阅读后,我认为这与 AsyncTask 的结构/参数有关,所以我尝试了一些变化,但没有任何乐趣 - 但我不确定我是否朝着正确的方向前进。我没有明确地在任何地方扩展应用程序。
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 to start service couk.jit.currencycheck1.ServiceClassXML@4602fc20 with 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): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3063)
09-04 09:13:46.444: E/AndroidRuntime(1190): at android.app.ActivityThread.access$3600(ActivityThread.java:125)
09-04 09:13:46.444: E/AndroidRuntime(1190): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2096)
09-04 09:13:46.444: E/AndroidRuntime(1190): at android.os.Handler.dispatchMessage(Handler.java:99)
09-04 09:13:46.444: E/AndroidRuntime(1190): at android.os.Looper.loop(Looper.java:123)
09-04 09:13:46.444: E/AndroidRuntime(1190): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-04 09:13:46.444: E/AndroidRuntime(1190): at java.lang.reflect.Method.invokeNative(Native Method)
09-04 09:13:46.444: E/AndroidRuntime(1190): at java.lang.reflect.Method.invoke(Method.java:521)
09-04 09:13:46.444: E/AndroidRuntime(1190): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-04 09:13:46.444: E/AndroidRuntime(1190): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-04 09:13:46.444: E/AndroidRuntime(1190): at 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): at couk.jit.currencycheck1.GetXMLTask.<init>(GetXMLTask.java:52)
09-04 09:13:46.444: E/AndroidRuntime(1190): at couk.jit.currencycheck1.ServiceClassXML.onStartCommand(ServiceClassXML.java:24)
09-04 09:13:46.444: E/AndroidRuntime(1190): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053)
09-04 09:13:46.444: E/AndroidRuntime(1190): ... 10 more
该服务在其下方运行 AsyncTask。
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;
}
}
有效的异步任务。
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;
}
}
这是给出错误的服务。如果我注释掉“newGetXMLTask”行,我不会收到错误消息。如果我用另一个 AsyncTask 替换它,我不会收到错误(但当然是后续错误)。日志消息显示在 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;
}
}
给出错误的 AsyncTask 的基础知识。
public class GetXMLTask extends AsyncTask<String, Void, List<RateData>> {
public GetXMLTask(Context ct, boolean asService) {
this.context = (Activity) ct; //as context can be cast to 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();
}
}
清单包括:
<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>