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

时间:2016-05-26 15:43:33

标签: c# listener sensormanager

我收到以下错误消息:

  

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

我尝试调试代码并发现传感器不为空!此类不是由Activity类派生的。谢谢你帮助我:)

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()
        {

        }

    }
}

0 个答案:

没有答案