【问题标题】:Xamarin OpenGL GLSurfaceView constructor throws null reference exceptionXamarin OpenGL GLSurfaceView 构造函数抛出空引用异常
【发布时间】:2017-03-25 13:33:26
【问题描述】:

我正在尝试使用 Xamarin opengl 实现 android LiveWallpaper。 但是当我激活 LiveWallpaper 时,GLSurfaceView 在构造函数中抛出空引用异常。

错误行: 调用 GLSurfaceView 的基本构造函数时的 base(context)。

错误详情: Java.Lang.NullPointerException:尝试调用接口方法 '无效的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;
    }
}

【问题讨论】:

    标签: c# xamarin opengl-es xamarin.android live-wallpaper


    【解决方案1】:

    我对 Holder 的覆盖做错了。不应覆盖持有人。

    取消这一行和相关的事情:
    公共覆盖 ISurfaceHolder Holder { get; }

    【讨论】:

      猜你喜欢
      • 2011-11-04
      • 2014-11-03
      • 1970-01-01
      • 1970-01-01
      • 2012-04-24
      • 1970-01-01
      • 2017-11-07
      • 2019-07-11
      • 2013-03-25
      相关资源
      最近更新 更多