我在Activity中有一个IntentService,当我尝试调用该服务时,它会抛出此错误,我发现这很奇怪,因为如果我声明了空构造函数.
错误:
06-17 15:48:34.603: E/AndroIDRuntime(13363): FATAL EXCEPTION: main06-17 15:48:34.603: E/AndroIDRuntime(13363): java.lang.RuntimeException: Unable to instantiate service cl.prosys.rac.activity.HomeActivity$UploadService: java.lang.InstantiationException: can't instantiate class cl.prosys.rac.activity.HomeActivity$UploadService; no empty constructor06-17 15:48:34.603: E/AndroIDRuntime(13363): at androID.app.ActivityThread.handleCreateService(ActivityThread.java:2380)06-17 15:48:34.603: E/AndroIDRuntime(13363): at androID.app.ActivityThread.access00(ActivityThread.java:134)06-17 15:48:34.603: E/AndroIDRuntime(13363): at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1306)06-17 15:48:34.603: E/AndroIDRuntime(13363): at androID.os.Handler.dispatchMessage(Handler.java:99)06-17 15:48:34.603: E/AndroIDRuntime(13363): at androID.os.Looper.loop(Looper.java:137)06-17 15:48:34.603: E/AndroIDRuntime(13363): at androID.app.ActivityThread.main(ActivityThread.java:4867)06-17 15:48:34.603: E/AndroIDRuntime(13363): at java.lang.reflect.Method.invokeNative(Native Method)06-17 15:48:34.603: E/AndroIDRuntime(13363): at java.lang.reflect.Method.invoke(Method.java:511)06-17 15:48:34.603: E/AndroIDRuntime(13363): at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)06-17 15:48:34.603: E/AndroIDRuntime(13363): at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:774)06-17 15:48:34.603: E/AndroIDRuntime(13363): at dalvik.system.NativeStart.main(Native Method)06-17 15:48:34.603: E/AndroIDRuntime(13363): Caused by: java.lang.InstantiationException: can't instantiate class cl.prosys.rac.activity.HomeActivity$UploadService; no empty constructor06-17 15:48:34.603: E/AndroIDRuntime(13363): at java.lang.class.newInstanceImpl(Native Method)06-17 15:48:34.603: E/AndroIDRuntime(13363): at java.lang.class.newInstance(Class.java:1319)06-17 15:48:34.603: E/AndroIDRuntime(13363): at androID.app.ActivityThread.handleCreateService(ActivityThread.java:2377)06-17 15:48:34.603: E/AndroIDRuntime(13363): ... 10 more
AndroIDManifest.xml中
<application> ... <service androID:name=".activity.HomeActivity$UploadService"></service></application>
活动:
package cl.prosys.rac.activity;import java.io.file;import java.util.ArrayList;import java.util.Map;import org.Json.JsONArray;import org.Json.JsONObject;import androID.app.IntentService;import androID.content.Intent;import androID.content.IntentFilter;import androID.database.Cursor;import androID.os.Bundle;import androID.os.Environment;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.AdapterVIEw;import androID.Widget.AdapterVIEw.OnItemClickListener;import androID.Widget.ArrayAdapter;import androID.Widget.ListVIEw;import cl.prosys.rac.R;import cl.prosys.rac.broadcast.UploadbroadcastReceiver;import cl.prosys.rac.config.AppConfig;import cl.prosys.rac.config.DataKey;import cl.prosys.rac.db.DataBase;import cl.prosys.rac.db.DataBaseFactory;import cl.prosys.rac.db.DataBasePrm;import cl.prosys.rac.holder.HomeItemmenuHolder;import cl.prosys.rac.net.Request;public class HomeActivity extends AppActivity { ... public voID onListItemClick(int position) { Intent intent; switch (position) { case 3: // Subir intent = new Intent(this, UploadService.class); startService(intent); break; } } public class UploadService extends IntentService { public UploadService() { super("UploadService"); } @OverrIDe protected voID onHandleIntent(Intent intent) { ... } }}
解决方法:
public class UploadService extends IntentService {
您的服务是一个内部阶级.如果要将其保留在Activity中,请将其更改为static:
public static class UploadService extends IntentService {
您可能想要阅读有关不同类型的嵌套类.来自谷歌的第一个链接:http://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
总结以上是内存溢出为你收集整理的java – intentservice没有空构造函数,但是有一个构造函数全部内容,希望文章能够帮你解决java – intentservice没有空构造函数,但是有一个构造函数所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)