您的位置:首页 > 其它

Andrioid开发过程中关于IntentService的问题

2015-09-01 20:14 337 查看
在Android开发中,笔者曾经遇到过

Unable to instantiate service

com.example.helloservice.HelloIntentService:

java.lang.InstantiationException: can’t instantiate class

com.example.helloservice.HelloIntentService; no empty constructor

这个问题,想了半天没找到解决办法。

后来查了书,原来是我的一个继承了IntentService的类使用了带参数的构造函数导致运行出错,

public class HelloIntentService extends IntentService {

public HelloIntentService(String name) {    //(1)
super(name);
// TODO 自动生成的构造函数存根
}

@Override
protected void onHandleIntent(Intent intent) {
// TODO 自动生成的方法存根
System.out.println("休息8秒");
try {
Thread.sleep(8000);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
//(1)部分的代码改为
public HelloIntentService() {//继承IntentService类必须要创建无参构造函数
super(“HelloIntentService”);
}


就顺利解决了问题啦!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: