错误 No enclosing instance of type WallpaperService is available due to some intermediate constructor invocation

引用:http://blog.csdn.net/You_and_Me12/article/details/7173825

No enclosing instance of type WallpaperService is available due to some intermediate constructor invocation


错误代码:

  1. import android.app.Service;  
  2. import android.os.Bundle;  
  3. import android.service.wallpaper.WallpaperService;  
  4. import android.service.wallpaper.WallpaperService.Engine;  
  5. import android.view.MotionEvent;  
  6. import android.view.SurfaceHolder;  
  7.   
  8. public class MyEngine extends Engine{  
  9.       
  10. ……  



才发现是继承了内隐类了,查找了一下

参考引文如下:点击打开链接

  1. 这是在《JAVA编程思想》上看到的一道例题,是关于继承inner   classes(内隐类)的。   
  2.   
  3. 先把代码和书上的一些原话写出来,如下:   
  4.   
  5. 由于inner   class的构造函数必须连接到一个reference指向outer   class   对象身上,所以   
  6. 当你继承inner   class时,事情便稍微复杂些。问题出在“指向outer   class对象”的那个   
  7. 神秘reference必须被初始化。但derived   class之内不存在可连接的缺省对象,这个问题   
  8. 的答案是,使用专用语法,明确产生该关联性:   
  9.   
  10. class   WithInner   {   
  11.         class   Inner{}   
  12. }   
  13.   
  14. public   class   InheritInner   extends   WithInner.Inner   {   
  15.         InheritInner(WithInner   wi)   {   
  16.                 wi.super();                   //---这里不懂,wi.super()指的是什么,有什么用?   
  17.         }   
  18.         public   static   void   main(String[]   args)   {   
  19.                 WithInner   wi   =   new   WithInner();   
  20.                 InheritInner   ii   =   new   InheritInner(wi);   
  21.         }   
  22. }   




正确修改后为:

  1. import android.app.Service;  
  2. import android.os.Bundle;  
  3. import android.service.wallpaper.WallpaperService;  
  4. import android.service.wallpaper.WallpaperService.Engine;  
  5. import android.view.MotionEvent;  
  6. import android.view.SurfaceHolder;  
  7.   
  8. public class MyEngine extends Engine{  
  9.       
  10.     public MyEngine(WallpaperService wp) {//添加的  
  11.         wp.super();  
  12.     }  
  13.     ……  
原文地址:https://www.cnblogs.com/sode/p/2319889.html