采取这个基类:
public abstract class XMPPSubservice
{
protected XMPPService mTheService;
protected XMPPSubservice(Context context)
{
Intent intent = new Intent(context, XMPPService.class);
context.startService(intent);
}
public void onServiceInstance(XMPPService service) {
// TODO Auto-generated method stub
mTheService = service;
}
}
而这个派生类:
public class PublicDataSubservice extends XMPPSubservice
{
private final SomeObject mObj = new SomeObject();
public PublicDataSubservice(Context context) {
super(context);
}
@Override
public void onServiceInstance(XMPPService service)
{
super.onServiceInstance(service);
mObj.doSomethingWith(mTheService);
}
}
目标是只调用 mObj.doSomethingWith(mTheService); 在 mTheService 生效之后(发生在基类中)。问题是它总是在 mObj 线上吐出 NPE。我可以理解为什么会这样,但对我来说这看起来很奇怪。那么这是 DVM 的错误还是功能?JVM怎么样?