内部的service报错(适用broadcast)
2016-12-20 15:31
323 查看
java.lang.InstantiationException:
class has no zero argument constructor
我为了共享定位信息把locationservice放到activity
首先是遇到了找不到service.class的问题Intent里面,mainifest文件声明service也报错,后来发现是需要声明为public。所以我就想非显式调用
5.0以上不允许非显式调用的问题
参考 http://blog.csdn.net/shenzhonglaoxu/article/details/42675287 查了网上解决方案
public static
Intent
createExplicitFromImplicitIntent(Context
context,
Intent
implicitIntent) {
// Retrieve
all services that can match the given intent
PackageManager
pm
=
context.getPackageManager();
List<ResolveInfo>
resolveInfo
=
pm.queryIntentServices(implicitIntent,
0);
// Make
sure only one match was found
if
(resolveInfo
==
null
||
resolveInfo.size()
!= 1) {
return
null;
}
// Get
component info and create ComponentName
ResolveInfo
serviceInfo
=
resolveInfo.get(0);
String
packageName
=
serviceInfo.serviceInfo.packageName;
String
className
=
serviceInfo.serviceInfo.name;
ComponentName
component
=
new
ComponentName(packageName,
className);
// Create
a new intent. Use the old one for extras and such reuse
Intent
explicitIntent
=
new
Intent(implicitIntent);
// Set
the component to be explicit
explicitIntent.setComponent(component);
return
explicitIntent;
}
转化
Intent
serviceIt
=
new
Intent(createExplicitFromImplicitIntent(this,tmpIntent));
startService(serviceIt);
最后解决了非显式问题后启动应用又报错:
java.lang.InstantiationException: class has no zero argument constructor
我看字面意思是没有无参构造函数
所以我自己添加了无参构造函数
结果还是报相同错
后来上网查资料发现 http://stackoverflow.com/questions/29947038/java-lang-instantiationexception-class-has-no-zero-argument-constructor inner Broadcast receiver must be static ( to be registered through Manifest)
所以我把这个locationService声明为static就好了
前后调试找资料花费了挺多时间,记录一下
class has no zero argument constructor
我为了共享定位信息把locationservice放到activity
首先是遇到了找不到service.class的问题Intent里面,mainifest文件声明service也报错,后来发现是需要声明为public。所以我就想非显式调用
5.0以上不允许非显式调用的问题
参考 http://blog.csdn.net/shenzhonglaoxu/article/details/42675287 查了网上解决方案
public static
Intent
createExplicitFromImplicitIntent(Context
context,
Intent
implicitIntent) {
// Retrieve
all services that can match the given intent
PackageManager
pm
=
context.getPackageManager();
List<ResolveInfo>
resolveInfo
=
pm.queryIntentServices(implicitIntent,
0);
// Make
sure only one match was found
if
(resolveInfo
==
null
||
resolveInfo.size()
!= 1) {
return
null;
}
// Get
component info and create ComponentName
ResolveInfo
serviceInfo
=
resolveInfo.get(0);
String
packageName
=
serviceInfo.serviceInfo.packageName;
String
className
=
serviceInfo.serviceInfo.name;
ComponentName
component
=
new
ComponentName(packageName,
className);
// Create
a new intent. Use the old one for extras and such reuse
Intent
explicitIntent
=
new
Intent(implicitIntent);
// Set
the component to be explicit
explicitIntent.setComponent(component);
return
explicitIntent;
}
转化
Intent
serviceIt
=
new
Intent(createExplicitFromImplicitIntent(this,tmpIntent));
startService(serviceIt);
最后解决了非显式问题后启动应用又报错:
java.lang.InstantiationException: class has no zero argument constructor
我看字面意思是没有无参构造函数
所以我自己添加了无参构造函数
结果还是报相同错
后来上网查资料发现 http://stackoverflow.com/questions/29947038/java-lang-instantiationexception-class-has-no-zero-argument-constructor inner Broadcast receiver must be static ( to be registered through Manifest)
所以我把这个locationService声明为static就好了
前后调试找资料花费了挺多时间,记录一下
相关文章推荐
- 请介绍下 AsyncTask 的内部实现,适用的场景,执行过程?
- Broadcast内部机制讲解
- Broadcast内部机制讲解 << 推荐
- 品茗论道说广播(Broadcast内部机制讲解)
- android broadcast, aidl和Content Provider的区别和适用场所
- 品茗论道说广播(Broadcast内部机制讲解)
- 如何使你的应用程序调试进.NET Framework 4.5源代码内部(适用B/S,C/S)
- Broadcast、Aidl、ContentProvider、Messager的区别和适用场景
- 如何使你的应用程序调试进.NET Framework 4.5源代码内部(适用B/S,C/S)
- AsyncTask的内部实现和适用场景
- (转)const的内部链接属性(C++中适用)
- 品茗论道说广播(Broadcast内部机制讲解)
- 关于BroadcastReceiver内部类
- broadcast, aidl和Content Provider的区别和适用场所?
- 广播(Broadcast内部机制讲解)
- 品茗论道说广播(Broadcast内部机制讲解)(上)
- (broadcast,intent,intentfilter消息广播)(interface接口,回调函数)(eventBus)区别,适用场合
- LocalBroadcastManager-应用内部的Broadcast
- Objective-C:MRC(引用计数器)在OC内部的可变对象是适用的,不可变对象是不适用的(例如 NSString、NSArray等)
- Android - Broadcast receiver 作为内部类接受广播