android注册监听器总是返回false,SensorEventListener保持为null,事件永远不会被触发



你好,我正在学习一些机器人编程,特别是为我正在尝试制作游戏的课程。我正试图使用方向传感器来获得在游戏中使用的输入,问题是我从未发生过onchange事件。我开始运行调试器,发现寄存器侦听器总是失败并返回false,这让我的传感器事件侦听器为null,我感到困惑。既然我已经做了13个小时了,我想我会忍气吞声,向你们寻求帮助。这是代码,我基本上已经把所有的东西都去掉了。我只是没有传感器。

谢谢大家给我的帮助

      package edu.hiram.cpsc172;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

@SuppressWarnings("deprecation")
public class GraphView extends View implements SensorEventListener 
{
    private Canvas screen;

    private boolean t = false;
    private float headingAngle;
    private float pitchAngle;
    private float rollAngle;
    private  SensorManager mSensorManager;
    private  Sensor mGyro;
    private SensorEventListener glisten;
    private Context context; // I have no idea what this actually means, but hey why not try? whats the worst that can happen?


       public GraphView(Context con) 
       {
           super(con);
           this.context = con; // so I have a context outside the constructor, it is needed for stuff.
                               // I find it amusing that although I have no clue what this stuff does I am 
                               // recognizing patterns in how context is used.
           //Annoyingly I have to do a few initializations in on draw, I dislike that. 
       }  


       // Called back to draw the view. Also called by invalidate()
       @Override
       protected void onDraw(Canvas canvas) 
       {
               //gives me a canvas object to play with if I want, and draws my background 
              screen = canvas;

            {
               mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
                  mGyro = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); 
        boolean x = true;        
         x =     mSensorManager.registerListener(glisten, mGyro, Sensor.TYPE_ORIENTATION); //this always returns false, and glisten stays null
                 x = x;
                 x=x; //I realize this does nothing, but it gives me places to set breakpoints, and check the line output
                 x = x;
            }


         try {  
                Thread.sleep(10);  
            } 
         catch (InterruptedException e) 
         {
         }
         invalidate();
      }



       @Override
        public void onSensorChanged(SensorEvent event)     
       {
           {
            headingAngle=event.values[0];
            pitchAngle= event.values[1];
            rollAngle=event.values[2];
           t= true;
           }
          Paint paint = new Paint();
          paint.setColor(Color.WHITE);
           screen.drawText("sensors responded", 100, 100, paint);
    }




    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) 
       {
        // TODO Auto-generated method stub
    }
}

更改线路后,侦听器将被注册,但onSensorChange从未启动。我希望编辑提问是对的,还是这是一个新问题?再次感谢

您从未初始化过glisten。但是,由于实现了接口,真正想做的是只使用this:

  mSensorManager.registerListener(this, mGyro, Sensor.TYPE_ORIENTATION);

请参阅示例。

相关内容

最新更新