c#[SensorManager]传感器或侦听器为空

c#[SensorManager]传感器或侦听器为空,c#,listener,sensormanager,C#,Listener,Sensormanager,我收到以下错误消息: [SensorManager]传感器或侦听器为空 我试着调试代码,发现传感器不是空的!此类不是由活动类派生的。谢谢你帮我:) 使用Magic.Util; 使用制度; 使用Android.Content; 使用Android.Hardware; 使用Magic.UI; 使用Microsoft.Xna.Framework.Graphics; 使用Microsoft.Xna.Framework; 使用Microsoft.Xna.Framework.Input; 使用Android

我收到以下错误消息:

[SensorManager]传感器或侦听器为空

我试着调试代码,发现传感器不是空的!此类不是由活动类派生的。谢谢你帮我:)

使用Magic.Util;
使用制度;
使用Android.Content;
使用Android.Hardware;
使用Magic.UI;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Input;
使用Android.OS;
名称空间魔术
{
公共类迷你游戏:场景,ISensorEventListener
{
专用传感器管理器mSensorManager=null;
上下文_Context=null;
//随机浮点值
随机;
//当前采矿集装箱位置
浮式xPositionMiningContainer;
//最大和最小值取决于显示大小
浮动最小值;
浮点最大值;
//设备大小
int设备宽度;
内部设备高度;
//UIElements
元素背景图像;
UIElement微型容器;
公共迷你游戏(场景管理器sM,上下文)
:基准(sM)
{
这._context=context;
deviceWidth=graphicsDevice.Viewport.Width;
deviceHeight=graphicsDevice.Viewport.Height;
场景管理器=sM;
初始化backgroundimage();
初始化容器();
//MSSensorManager=context.GetSystemService(context.SensorService)作为SensorManager;
加速度计启动();
}
public void AccelerometerStart(){
MSSensorManager=_context.GetSystemService(context.SensorService)作为SensorManager;
if(MSSensorManager.GetDefaultSensor(SensorType.Accelerator)!=null){
Console.WriteLine(“成功!有加速器。”);
}
否则{
Console.WriteLine(“失败!没有加速器”);
}
如果(mSensorManager!=null)
{
Console.WriteLine(“MSSensorManager不为空!”);
传感器传感器=MSSensorManager.GetDefaultSensor(传感器类型.加速计);
mSensorManager.RegisterListener(this、sensor、SensorDelay.Ui);
}
其他的
{
抛出新异常(“无法加载加速计传感器”);
}
}
公共空间加速度计cancel()
{
mSensorManager.UnregisterListener(此);
}
精度更改时的公共无效(传感器、传感器状态传感器状态)
{
}
传感器更改时的公共无效(传感器事件e)
{
Console.WriteLine(string.Format(“x={0:f},y={1:f},y={2:f}”),e.Values[0],e.Values[1],e.Values[2]);
}
/// 
///初始化背景图像。
/// 
public void InitializeBackgroundImage()
{
Console.WriteLine(“欢迎来到MiningMiniGame场景”);
//设置场景的背景图像
backgroundImage=新的UIElement(这是一个新的矩形(0,0,deviceWidth,deviceHeight));
backgroundImage.SetBackgroundTexture(Resources.Content.Load(“Images/UI/Backgrounds/main_background”);
添加元素(背景图像);
}
/// 
///初始化挖掘容器,在其中收集下落的东西。
/// 
///采矿容器。
public void InitializeMiningContainer()
{
miningContainer=新的UIElement(这是一个新的矩形((int)(deviceWidth*0.375),(int)(deviceHeight*0.8),(int)(deviceWidth*0.25),(int)(deviceHeight*0.2));
miningContainer.SetBackgroundTexture(Resources.Content.Load(“image/UI/Buttons/mine”);
miningContainer.SetID(“miningContainer”);
//miningContainer.SetOnClickMethod(委托()
//              {
//                  
//              });
AddUIElement(miningContainer);
}
/// 
///从挖掘容器更新x位置。
/// 
public void UpdatePositionMiningContainer(float xPosition){
miningContainer.Position.X=xPosition;
}
/// 
///使用指定的游戏时间更新场景。
/// 
///比赛时间到了。
公共覆盖无效更新(Microsoft.Xna.Framework.GameTime游戏时间)
{
更新(游戏时间);
把手按钮();
}
/// 
///使用指定的游戏时间绘制场景。
/// 
///比赛时间到了。
公共覆盖无效绘制(Microsoft.Xna.Framework.GameTime游戏时间)
{
图形设备。清晰(颜色。白色);
基础。抽签(游戏时间);
}
/// 
///处理后退按钮。
/// 
专用无效把手按钮()
{
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed){
WriteLine(“点击了小游戏中的硬件备份按钮!”);
SceneManager.SwitchToOldScene();
}
}
/// 
///创建随机X位置。
/// 
///随机X位置。
公共浮点createRandomXPosition()
{
最小值=0;
maxValue=graphicsDevice.Viewport.Width;
返回((float)random.NextDouble())*(maxValue-minValue)+minValue;
}
公共IntPtr句柄
{ 
得到;
}
/// 
///释放对象使用的所有资源。
/// 
///完成使用后,请致电。的
///方法使处于不可用状态。调用
///,则必须释放对的所有引用,以便垃圾
///收集器可以回收正在占用的内存。
公共覆盖无效Dispo
using Magic.Util;
using System;
using Android.Content;
using Android.Hardware;
using Magic.UI;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Android.OS;



namespace Magic
{

    public class MiningMinigame : Scene, ISensorEventListener
    {

        private SensorManager mSensorManager = null;

        Context _context = null;

        //Random float value
        Random random;

        //Current mining Container Position
        float xPositionMiningContainer;

        //max & min value depends on the displaysize
        float minValue;
        float maxValue;

        //device sizes
        int deviceWidth;
        int deviceHeight;

        //UIElements
        UIElement backgroundImage;
        UIElement miningContainer;


        public MiningMinigame (SceneManager sM, Context context)
            : base(sM)
        {
            this._context = context;
            deviceWidth = graphicsDevice.Viewport.Width;
            deviceHeight = graphicsDevice.Viewport.Height;
            SceneManager = sM;
            InitializeBackgroundImage ();
            InitializeMiningContainer ();

            //mSensorManager = context.GetSystemService(Context.SensorService) as SensorManager;
            AccelerometerStart ();
        }

        public void AccelerometerStart() { 
            mSensorManager = _context.GetSystemService(Context.SensorService) as SensorManager;
            if (mSensorManager.GetDefaultSensor(SensorType.Accelerometer) != null){
                Console.WriteLine ("Success! There's a Accelerator.");
            }
            else {
                Console.WriteLine ("Failure! No Accelerator.");
            }

            if (mSensorManager != null)
            {
                Console.WriteLine ("mSensorManager is not null!");
                Sensor sensor = mSensorManager.GetDefaultSensor(SensorType.Accelerometer);
                mSensorManager.RegisterListener(this, sensor, SensorDelay.Ui);
            }
            else 
            {
                throw new Exception("Could not load Accelerometer sensor"); 
            }
        }

        public void AccelerometerCancel() 
        {
            mSensorManager.UnregisterListener(this); 
        }

        public void OnAccuracyChanged(Sensor sensor, SensorStatus sensorStatus) 
        {
        }

        public void OnSensorChanged(SensorEvent e) 
        {
            Console.WriteLine (string.Format("x={0:f}, y={1:f}, y={2:f}", e.Values[0], e.Values[1], e.Values[2]));
        }

        /// <summary>
        /// Initializes the background image.
        /// </summary>
        public void InitializeBackgroundImage() 
        {
            Console.WriteLine("Welcome in the MiningMiniGame Scene");

            //Sets the backgroundimage of the Scene
            backgroundImage = new UIElement(this, new Rectangle(0, 0, deviceWidth, deviceHeight));
            backgroundImage.SetBackgroundTexture(Resources.Content.Load<Texture2D>("Images/UI/Backgrounds/main_background"));
            AddUIElement(backgroundImage);
        }

        /// <summary>
        /// Initializes the mining container, where you collect the downfalling stuff.
        /// </summary>
        /// <returns>The mining container.</returns>
        public void InitializeMiningContainer()
        {
            miningContainer = new UIElement (this, new Rectangle ((int)(deviceWidth * 0.375), (int)(deviceHeight * 0.8), (int)(deviceWidth * 0.25), (int)(deviceHeight * 0.2)));
            miningContainer.SetBackgroundTexture (Resources.Content.Load<Texture2D> ("Images/UI/Buttons/mine"));
            miningContainer.SetID ("MiningContainer");
//          miningContainer.SetOnClickMethod (delegate() 
//              {
//                  
//              });
            AddUIElement (miningContainer);
        }

        /// <summary>
        /// Updates the x-position from the mining container.
        /// </summary>
        public void UpdatePositionMiningContainer(float xPosition) {
            miningContainer.Position.X = xPosition;
        }


        /// <summary>
        /// Update the scene using the specified gameTime.
        /// </summary>
        /// <param name="gameTime">Game time.</param>
        public override void Update (Microsoft.Xna.Framework.GameTime gameTime)
        {
            base.Update (gameTime);
            HandleBackButton();
        }

        /// <summary>
        /// Draw the scene using the specified gameTime.
        /// </summary>
        /// <param name="gameTime">Game time.</param>
        public override void Draw (Microsoft.Xna.Framework.GameTime gameTime)
        {
            graphicsDevice.Clear (Color.White);
            base.Draw (gameTime);
        }

        /// <summary>
        /// Handles the back button.
        /// </summary>
        private void HandleBackButton()
        {
            if(GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed){
                Console.WriteLine("HardwareBackButton in MiniGame was clicked!");
                SceneManager.SwitchToOldScene();
            }
        }

        /// <summary>
        /// Creates the random X position.
        /// </summary>
        /// <returns>The random X position.</returns>
        public float createRandomXPosition()
        {
            minValue = 0;
            maxValue = graphicsDevice.Viewport.Width;
            return ((float)random.NextDouble ()) * (maxValue - minValue) + minValue;
        }

        public IntPtr Handle 
        { 
            get; 
        }

        /// <summary>
        /// Releases all resource used by the <see cref="Magic.MiningMinigame"/> object.
        /// </summary>
        /// <remarks>Call <see cref="Dispose"/> when you are finished using the <see cref="Magic.MiningMinigame"/>. The
        /// <see cref="Dispose"/> method leaves the <see cref="Magic.MiningMinigame"/> in an unusable state. After calling
        /// <see cref="Dispose"/>, you must release all references to the <see cref="Magic.MiningMinigame"/> so the garbage
        /// collector can reclaim the memory that the <see cref="Magic.MiningMinigame"/> was occupying.</remarks>
        public override void Dispose()
        {

        }

    }
}