今天想要繼承android裡面的一個類,但是出現了這個問題:
No enclosing instance of type WallpaperService is available due to some intermediate constructor
invocation
錯誤碼:
import android.app.Service;import android.os.Bundle;import android.service.wallpaper.WallpaperService;import android.service.wallpaper.WallpaperService.Engine;import android.view.MotionEvent;import android.view.SurfaceHolder;public class MyEngine extends Engine{……
才發現是繼承了內隱類了,尋找了一下
參考引文如下:點擊開啟連結
這是在《JAVA編程思想》上看到的一道例題,是關於繼承inner classes(內隱類)的。 先把代碼和書上的一些原話寫出來,如下: 由於inner class的建構函式必須串連到一個reference指向outer class 對象身上,所以 當你繼承inner class時,事情便稍微複雜些。問題出在“指向outer class對象”的那個 神秘reference必須被初始化。但derived class之內不存在可串連的預設對象,這個問題 的答案是,使用專用文法,明確產生該關聯性: class WithInner { class Inner{} } public class InheritInner extends WithInner.Inner { InheritInner(WithInner wi) { wi.super(); //---這裡不懂,wi.super()指的是什麼,有什麼用? } public static void main(String[] args) { WithInner wi = new WithInner(); InheritInner ii = new InheritInner(wi); } }
正確修改後為:
import android.app.Service;import android.os.Bundle;import android.service.wallpaper.WallpaperService;import android.service.wallpaper.WallpaperService.Engine;import android.view.MotionEvent;import android.view.SurfaceHolder;public class MyEngine extends Engine{public MyEngine(WallpaperService wp) {//添加的wp.super();}……