我正在尝试用Xamarin opengl实现android LiveWallpaper。 但是当我激活LiveWallpaper时,GLSurfaceView会在构造函数中抛出null引用异常。
错误行: 调用GLSurfaceView的基础构造函数时的base(context)。
错误详情: Java.Lang.NullPointerException:尝试调用接口方法 'void android.view.SurfaceHolder.addCallback(android.view.SurfaceHolder $ Callback)' 在空对象引用上
[Service(Label = "@string/ApplicationName", Permission = "android.permission.BIND_WALLPAPER")]
[IntentFilter(new[] { "android.service.wallpaper.WallpaperService" })]
[MetaData("android.service.wallpaper", Resource = "@layout/MyLiveWallpaper")]
public class OpenGLWallpaperService : WallpaperService
{
public override Engine OnCreateEngine()
{
return new OpenGLWallpaperEngine(this);
}
public class OpenGLWallpaperEngine : Engine
{
private readonly OpenGLWallpaperService _serviceInstance;
public OpenGLWallpaperView GLWallpaperView { get; set; }
public OpenGLWallpaperEngine(OpenGLWallpaperService serviceInstance)
: base(serviceInstance)
{
_serviceInstance = serviceInstance;
}
public override void OnCreate(ISurfaceHolder surfaceHolder)
{
base.OnCreate(surfaceHolder);
GLWallpaperView = new OpenGLWallpaperView(this, _serviceInstance, surfaceHolder);
}
}
}
public class OpenGLWallpaperView : GLSurfaceView
{
private readonly OpenGLWallpaperService.OpenGLWallpaperEngine _engineInstance;
public override ISurfaceHolder Holder { get; }
public OpenGLWallpaperView(OpenGLWallpaperService.OpenGLWallpaperEngine engineInstance,
Context context, ISurfaceHolder surfaceHolder)
: base(context)
{
_engineInstance = engineInstance;
Holder = surfaceHolder;
}
}
答案 0 :(得分:0)
我对覆盖Holder做错了。持有人不应被覆盖。
取消此行及相关事项:
公开覆盖ISurfaceHolder Holder {get; }