android - 为什么哔声会导致应用程序崩溃?

标签 android crash handler accelerometer ontouchlistener

我尝试制作一个水平指示器,当手机到达水平位置(使用加速度计)时发出更快的哔哔声,在触摸屏幕后发出哔哔声,如果再次触摸屏幕则停止。
所以我编写了以下代码,我遇到的问题是,由于我添加了“哔”声部分(在 onTouch 事件中调用了 MyRunnable 函数),我的应用程序在几秒钟后崩溃(它可以在几秒钟内正常工作并且构建后我没有错误消息)。
我真的不知道问题可能是什么。我被困在这里,需要一些帮助,谢谢!
公共(public)类 MainActivity 扩展 AppCompatActivity 实现 SensorEventListener,View.OnTouchListener {

Sensor mySensor;
SensorManager SM;

float ANGLEX, ANGLEY, ANGLEZ;
int aX, aY, aZ;
int roundANGLEX, roundANGLEY, roundANGLEZ;
int Xetal, Yetal;
double centre;
int Rcentre;
int Rcentre2;
boolean active;
int test;

int i=0;
private Handler myHandler;

private Runnable myRunnable = new Runnable() {
    @Override
    public void run() {
        // Play a periodic beep which accelerates according to Rcentre2

        ToneGenerator toneGen1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
        toneGen1.startTone(ToneGenerator.TONE_PROP_BEEP,150);
        myHandler.postDelayed(this,(long) Math.sqrt(Rcentre2*20)+50);
    }
};

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Créée notre Sensor Manager
    SM = (SensorManager) getSystemService(SENSOR_SERVICE);

    //Accéléromètre
    mySensor = SM.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    //Registre Sensor Listener
    SM.registerListener(this, mySensor, 1000000, 1000000);


    ((ConstraintLayout) findViewById(R.id.layout_main)).setOnTouchListener((View.OnTouchListener) this);
}



@Override
public void onAccuracyChanged(Sensor sensor, int i) {
    //Pas utilisé
}


@Override
public void onSensorChanged(SensorEvent sensorEvent) {

    float z_stable = ((int) ((sensorEvent.values[2]) * 100)) / 100.0f;


    ANGLEX = (float) (((float) (Math.acos(sensorEvent.values[0] / 9.807)) * (180 / Math.PI))); //I get the accelerometer's values
    ANGLEY = (float) (((float) (Math.acos(sensorEvent.values[1] / 9.807)) * (180 / Math.PI)));
    ANGLEZ = (float) (((float) (Math.acos(z_stable / 9.807)) * (180 / Math.PI)));

    roundANGLEX = Math.round(ANGLEX);
    roundANGLEY = Math.round(ANGLEY);
    roundANGLEZ = Math.round(ANGLEZ);

    aX = roundANGLEX;
    aY = roundANGLEY;
    aZ = roundANGLEZ;


    Xetal = aX - 88; //Xetal and Yetal are 0 when phone is on plane surface
    Yetal = aY - 90; //and go from -90 to +90

    centre = Math.sqrt(Xetal * Xetal + Yetal * Yetal); //gives the "distance" from the "center => the smaller centre gets, the closer the phone approach horizontal
    Rcentre = (int) Math.round(centre);
    Rcentre2 = (int) Math.round(centre * 100);
}


public boolean onTouch(View view, MotionEvent motionEvent) {
    if (active == true) {
        active = false;
        myHandler.removeCallbacks(myRunnable);
    }
    else if (active == false) {
        active = true;

            myHandler = new Handler();
            myHandler.postDelayed(myRunnable,0); 

        }
    return false;
}
}
这是 Logcat 信息,它似乎显示了一些问题,但我不知道这意味着什么。
logcat information

最佳答案

我在 ToneGenerator crashes in android 6.0 找到了适合我的答案

“这只是释放创建的 ToneGenerator 对象,因为快速创建 'ToneGenerator' 的对象而不释放它们会导致应用程序崩溃。”

        ToneGenerator toneGen1 = new ToneGenerator(AudioManager.STREAM_MUSIC, 100);
        toneGen1.startTone(ToneGenerator.TONE_PROP_BEEP,150);
        toneGen1.release();

我添加了toneGen1.release();现在它工作正常。

归功于马哈茂德·法拉哈特。

关于android - 为什么哔声会导致应用程序崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49296102/

相关文章:

android - Android 中用于图像过渡的动画

android - Toast.makeText 未在 Android 11 中显示(使用 SDK 30 模拟器)

azure - 升级到 asp.net core rc2 后发布到 Azure 应用服务崩溃

silverlight - 为什么当我将绑定(bind)添加到 TextBlock 时 Visual Studio 2010 会崩溃?

Android 检查某些平板电脑上的网络连接崩溃

python - 使用类结构和 Python 协议(protocol)删除窗口处理程序

android - Android Studio 3.0 不再在输出文件夹中创建 AAR 库

java - Android:OpenGL:何时加载纹理?

android - AsyncTask 真的在概念上存在缺陷还是我只是遗漏了什么?

c - 信号终止处理程序和清理操作