Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:双数组中的乘法值_Java_Android_Arrays_Sqlite_Android Sensors - Fatal编程技术网

Java:双数组中的乘法值

Java:双数组中的乘法值,java,android,arrays,sqlite,android-sensors,Java,Android,Arrays,Sqlite,Android Sensors,我正在开发一个Android应用程序,它将传感器数据记录到SQLite数据库中 程序如下所示: 将传感器的所有值添加到双阵列。每个维度一个双数组,例如,对于加速计,我有3个双数组(一个用于x值,一个用于y值,一个用于z值) 当所有值都在双数组中时,请将双数组转换为字符串 将字符串放入SQLite表中的相关列 当我将每个双数组中的值相乘时,我遇到了一个问题 下面是代码片段: @Override public void onSensorChanged(final SensorEvent sensor

我正在开发一个Android应用程序,它将传感器数据记录到SQLite数据库中

程序如下所示:

  • 将传感器的所有值添加到双阵列。每个维度一个双数组,例如,对于加速计,我有3个双数组(一个用于x值,一个用于y值,一个用于z值)
  • 当所有值都在双数组中时,请将双数组转换为字符串
  • 将字符串放入SQLite表中的相关列
  • 当我将每个双数组中的值相乘时,我遇到了一个问题

    下面是代码片段:

    @Override
    public void onSensorChanged(final SensorEvent sensorEvent) {
    
        if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            //Getting and displaying accelerometer data
            x = sensorEvent.values[0];
            y = sensorEvent.values[1];
            z = sensorEvent.values[2];
        }
    
        if (sensorEvent.sensor.getType() == Sensor.TYPE_GYROSCOPE) {
            //Getting and displaying gyroscope data
            x2 = sensorEvent.values[0];
            y2 = sensorEvent.values[1];
            z2 = sensorEvent.values[2];
        }
    
        if (sensorEvent.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
            //Getting and displaying gyroscope data
            x3 = sensorEvent.values[0]; // x*sin(θ/2)
            y3 = sensorEvent.values[1]; // y*sin(θ/2)
            z3 = sensorEvent.values[2]; // z*sin(θ/2)
            cos3 = sensorEvent.values[3]; // cos(θ/2)
            accuracy3 = sensorEvent.values[4]; // estimated heading Accuracy (in radians) (-1 if unavailable)
        }
    
        //Start recording sensor data on click
        Button startButton = (Button) findViewById(R.id.buttonStart);
        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
    
                //Get label from EditText
                label = textLabel.getText().toString();
    
                statusText.setText("Wait");
                statusText.setTextColor(Color.RED);
    
                //Get current date and time
                currentTime = Calendar.getInstance().getTime();
                dateTime = currentTime.toString();
    
                flag = true; //flag set to true = record data
                samples = 0;
                startTime = System.currentTimeMillis();
            }
        });
    
    
        //Insert given number of sensor data samples into database
        if (samples < samples_total) {
            if (flag == true) {
    
                //Get current date and time
                currentTime = Calendar.getInstance().getTime();
                dateTime = currentTime.toString();
    
                //Add x,y,z values to arrays for sensors
                accDoubleArrayX[samples] = x;
                accDoubleArrayY[samples] = y;
                accDoubleArrayZ[samples] = z;
    
                gyroDoubleArrayX[samples] = x2;
                gyroDoubleArrayY[samples] = y2;
                gyroDoubleArrayZ[samples] = z2;
    
                rotationDoubleArrayX[samples] = x3;
                rotationDoubleArrayY[samples] = y3;
                rotationDoubleArrayZ[samples] = z3;
                rotationDoubleArrayCos[samples] = cos3;
                rotationDoubleArrayAccuracy[samples] = accuracy3;
    
                samples++;
    
            } else {
                //Log.d("", "samples < total but Flag is false - Data NOT inserted");
            }
        } else if (samples == samples_total) {
            durationTime = System.currentTimeMillis() - startTime;
    
            //Insert x,y,z values for sensors
            dbRef.insertData(dateTime, accDoubleArrayX, accDoubleArrayY, accDoubleArrayZ,
                    gyroDoubleArrayX, gyroDoubleArrayY, gyroDoubleArrayZ,
                    rotationDoubleArrayX, rotationDoubleArrayY, rotationDoubleArrayZ, rotationDoubleArrayCos, rotationDoubleArrayAccuracy,
                    durationTime, label);
    
            Log.d("", "Data inserted!");
            Toast.makeText(this, "Data inserted!", Toast.LENGTH_LONG).show();
    
            //Stop recording when given number of samples inserted
            flag = false;
            samples++;
    
        } else {
            //Not in use
        }
    
    }
    
    这段代码的工作原理几乎和预期的一样,但给了我成倍的值。例如,数组中的值如下所示:

    accDoubleArrayX: [2.013523578643799, 2.013523578643799, 2.013523578643799, 2.492363929748535, 2.492363929748535, 2.492363929748535, -7.771578788757324, -7.771578788757324, -7.771578788757324, 2.0913352966308594, 2.0913352966308594, 2.0913352966308594, 9.205705642700195, 9.205705642700195, 9.205705642700195, -2.649184226989746, -2.649184226989746, -2.649184226989746, 0.851138710975647, 0.851138710975647, 0.851138710975647, 1.4508862495422363, 1.4508862495422363, 1.4508862495422363, 3.1938650608062744, 3.1938650608062744, 3.1938650608062744, 1.7621324062347412, 1.7621324062347412, 1.7621324062347412, -5.260061264038086, -5.260061264038086]   
    accDoubleArrayY: [5.456385612487793, 5.456385612487793, 5.456385612487793, 7.893682956695557, 7.893682956695557, 7.893682956695557, 3.6786909103393555, 3.6786909103393555, 3.6786909103393555, 2.8095955848693848, 2.8095955848693848, 2.8095955848693848, 6.449979305267334, 6.449979305267334, 6.449979305267334, 5.60602331161499, 5.60602331161499, 5.60602331161499, 5.076904773712158, 5.076904773712158, 5.076904773712158, -0.05985504388809204, -0.05985504388809204, -0.05985504388809204, 2.78086519241333, 2.78086519241333, 2.78086519241333, 6.861782073974609, 6.861782073974609, 6.861782073974609, 4.49870491027832, 4.49870491027832]    
    accDoubleArrayZ: [18.986019134521484, 18.986019134521484, 18.986019134521484, 0.5770025849342346, 0.5770025849342346, 0.5770025849342346, 6.994660377502441, 6.994660377502441, 6.994660377502441, 13.217190742492676, 13.217190742492676, 13.217190742492676, 2.6240451335906982, 2.6240451335906982, 2.6240451335906982, -9.330204010009766, -9.330204010009766, -9.330204010009766, 9.876082420349121, 9.876082420349121, 9.876082420349121, 13.722367286682129, 13.722367286682129, 13.722367286682129, 4.235342979431152, 4.235342979431152, 4.235342979431152, -10.040084838867188, -10.040084838867188, -10.040084838867188, -1.8638859987258911, -1.8638859987258911]   
    
    gyroDoubleArrayX: [-0.575242817401886, 0.14913702011108398, 0.14913702011108398, 0.14913702011108398, 2.141181468963623, 2.141181468963623, 2.141181468963623, -4.218447208404541, -4.218447208404541, -4.218447208404541, -0.030892670154571533, -0.030892670154571533, -0.030892670154571533, 5.554288864135742, 5.554288864135742, 5.554288864135742, -2.211488962173462, -2.211488962173462, -2.211488962173462, -5.499960422515869, -5.499960422515869, -5.499960422515869, -1.3646037578582764, -1.3646037578582764, -1.3646037578582764, 7.179882526397705, 7.179882526397705, 7.179882526397705, 1.353951096534729, 1.353951096534729, 1.353951096534729, -5.861084938049316]   
    gyroDoubleArrayY: [-0.4165183901786804, -1.6266587972640991, -1.6266587972640991, -1.6266587972640991, -1.0279802083969116, -1.0279802083969116, -1.0279802083969116, 3.4663705825805664, 3.4663705825805664, 3.4663705825805664, -7.069095134735107, -7.069095134735107, -7.069095134735107, 0.3334277868270874, 0.3334277868270874, 0.3334277868270874, 2.221076488494873, 2.221076488494873, 2.221076488494873, -0.21944448351860046, -0.21944448351860046, -0.21944448351860046, -3.3257555961608887, -3.3257555961608887, -3.3257555961608887, 0.04580637067556381, 0.04580637067556381, 0.04580637067556381, 0.3419498801231384, 0.3419498801231384, 0.3419498801231384, 1.2857742309570312]  
    gyroDoubleArrayZ: [0.0649811327457428, -0.18002969026565552, -0.18002969026565552, -0.18002969026565552, 0.2865561246871948, 0.2865561246871948, 0.2865561246871948, -0.7233145833015442, -0.7233145833015442, -0.7233145833015442, -0.6796387434005737, -0.6796387434005737, -0.6796387434005737, -1.1739214658737183, -1.1739214658737183, -1.1739214658737183, -3.5452001094818115, -3.5452001094818115, -3.5452001094818115, -0.014913702383637428, -0.014913702383637428, -0.014913702383637428, -0.6348975896835327, -0.6348975896835327, -0.6348975896835327, -0.9310411214828491, -0.9310411214828491, -0.9310411214828491, -2.379800796508789, -2.379800796508789, -2.379800796508789, -0.6317018270492554]    
    
    rotationDoubleArrayX: [0.2823379933834076, 0.2823379933834076, 0.26849400997161865, 0.26849400997161865, 0.26849400997161865, 0.2820430099964142, 0.2820430099964142, 0.2820430099964142, 0.35002198815345764, 0.35002198815345764, 0.35002198815345764, 0.20867300033569336, 0.20867300033569336, 0.20867300033569336, 0.2366899996995926, 0.2366899996995926, 0.2366899996995926, 0.39944401383399963, 0.39944401383399963, 0.39944401383399963, 0.3354569971561432, 0.3354569971561432, 0.3354569971561432, 0.17095300555229187, 0.17095300555229187, 0.17095300555229187, 0.1272200047969818, 0.1272200047969818, 0.1272200047969818, 0.34220200777053833, 0.34220200777053833, 0.34220200777053833]    
    rotationDoubleArrayY: [0.10564299672842026, 0.10564299672842026, 0.08853700011968613, 0.08853700011968613, 0.08853700011968613, 0.04211900010704994, 0.04211900010704994, 0.04211900010704994, 0.01869099959731102, 0.01869099959731102, 0.01869099959731102, 0.1025020033121109, 0.1025020033121109, 0.1025020033121109, -0.10204900056123734, -0.10204900056123734, -0.10204900056123734, -0.06824100017547607, -0.06824100017547607, -0.06824100017547607, 0.03044299967586994, 0.03044299967586994, 0.03044299967586994, 0.026799000799655914, 0.026799000799655914, 0.026799000799655914, -0.07169300317764282, -0.07169300317764282, -0.07169300317764282, -0.07498300075531006, -0.07498300075531006, -0.07498300075531006]  
    rotationDoubleArrayZ: [0.19162000715732574, 0.19162000715732574, 0.18857499957084656, 0.18857499957084656, 0.18857499957084656, 0.16563500463962555, 0.16563500463962555, 0.16563500463962555, 0.15753500163555145, 0.15753500163555145, 0.15753500163555145, 0.16617600619792938, 0.16617600619792938, 0.16617600619792938, 0.0989999994635582, 0.0989999994635582, 0.0989999994635582, 0.0755779966711998, 0.0755779966711998, 0.0755779966711998, -0.011357000097632408, -0.011357000097632408, -0.011357000097632408, -0.0029909999575465918, -0.0029909999575465918, -0.0029909999575465918, -0.039792999625205994, -0.039792999625205994, -0.039792999625205994, -0.05465399846434593, -0.05465399846434593, -0.05465399846434593]    
    rotationDoubleArrayCos: [0.9340260028839111, 0.9340260028839111, 0.9404839873313904, 0.9404839873313904, 0.9404839873313904, 0.94405597448349, 0.94405597448349, 0.94405597448349, 0.9232100248336792, 0.9232100248336792, 0.9232100248336792, 0.9582970142364502, 0.9582970142364502, 0.9582970142364502, 0.9611250162124634, 0.9611250162124634, 0.9611250162124634, 0.911083996295929, 0.911083996295929, 0.911083996295929, 0.9414939880371094, 0.9414939880371094, 0.9414939880371094, 0.9849100112915039, 0.9849100112915039, 0.9849100112915039, 0.9884790182113647, 0.9884790182113647, 0.9884790182113647, 0.9350330233573914, 0.9350330233573914, 0.9350330233573914] 
    rotationDoubleArrayAccuracy: [246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0, 246.0]   
    
    如您所见,加速度计的前3个值相同,下3个值相同,依此类推。 对于陀螺仪:第一个值是唯一的,然后三个值相同,接下来的三个值相同,依此类推。 对于旋转向量:前两个相同,然后下三个相同,然后下三个相同,依此类推

    对我来说,这似乎真的很奇怪,我找不到解决这个问题的办法


    您能帮助我吗?

    这是因为每次接收到传感器事件时,您都会为所有3种事件类型填充数组,而不是只为一种事件类型(接收到事件的类型)填充数组

    为了避免这种情况,您需要为每个事件类型保留三个单独的计数器

    另外,据我所知,您希望在所有样本数组都被完全填充后将它们插入数据库。因此,您还需要修改插入DB的条件

    因此,填充例程应修改如下:

    if (samples_acc < samples_total || samples_gyro < samples_total || samples_rotation < samples_total) 
                    {
                                if (flag == true) 
                                {
    
                                    //Get current date and time
                                    currentTime = Calendar.getInstance().getTime();
                                    dateTime = currentTime.toString();
    
                                    //Add x,y,z values to arrays for sensors
    
                                    if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER && samples_acc < samples_total) 
                                    {
                                    accDoubleArrayX[samples_acc] = x;
                                    accDoubleArrayY[samples_acc] = y;
                                    accDoubleArrayZ[samples_acc] = z;
    
                                    samples_acc++;
                                    }
    
                                    if (sensorEvent.sensor.getType() == Sensor.TYPE_GYROSCOPE && samples_gyro < samples_total) 
                                    {
                                    gyroDoubleArrayX[samples_gyro] = x2;
                                    gyroDoubleArrayY[samples_gyro] = y2;
                                    gyroDoubleArrayZ[samples_gyro] = z2;
    
                                    samples_gyro++;
                                    }
    
                                    if (sensorEvent.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR && samples_rotation < samples_total) 
                                    {
                                    rotationDoubleArrayX[samples_rotation] = x3;
                                    rotationDoubleArrayY[samples_rotation] = y3;
                                    rotationDoubleArrayZ[samples_rotation] = z3;
                                    rotationDoubleArrayCos[samples_rotation] = cos3;
                                    rotationDoubleArrayAccuracy[samples_rotation] = accuracy3;
    
                                    samples_rotation++;
                                    }
    
                                }//end if flag=true
                                else 
                                {
                                //Log.d("", "samples < total but Flag is false - Data NOT inserted");
                                }
                }//end if at least one of arrays is not filled yet
    
                //all arrays have been filled   
                else 
                {
                    durationTime = System.currentTimeMillis() - startTime;
    
                    //Insert x,y,z values for sensors
                    dbRef.insertData(dateTime, accDoubleArrayX, accDoubleArrayY, accDoubleArrayZ,
                            gyroDoubleArrayX, gyroDoubleArrayY, gyroDoubleArrayZ,
                            rotationDoubleArrayX, rotationDoubleArrayY, rotationDoubleArrayZ, rotationDoubleArrayCos, rotationDoubleArrayAccuracy,
                            durationTime, label);
    
                    Log.d("", "Data inserted!");
                    Toast.makeText(this, "Data inserted!", Toast.LENGTH_LONG).show();
    
                    //Stop recording when given number of samples inserted
                    flag = false;
                }
    
    if(样本_acc<样本|总数|样本|陀螺<样本|总数|样本|旋转<样本|总数)
    {
    如果(标志==真)
    {
    //获取当前日期和时间
    currentTime=Calendar.getInstance().getTime();
    dateTime=currentTime.toString();
    //将x、y、z值添加到传感器阵列
    if(sensorEvent.sensor.getType()==sensor.TYPE\u加速计和样本\u acc

    哦,还有一个关于按钮的附加说明:您最好将其移出您的
    onSensorChanged
    方法,因为每次接收到新事件时重新声明它是没有意义的

    此代码在插入之前添加了所有传感器样本必须等于样本总数的条件后生效。将标志设置为false increment+1后,要求所有传感器样本不落入插入循环。但是非常感谢,感谢你的回答,我可以继续我的编程:)
    if (samples_acc < samples_total || samples_gyro < samples_total || samples_rotation < samples_total) 
                    {
                                if (flag == true) 
                                {
    
                                    //Get current date and time
                                    currentTime = Calendar.getInstance().getTime();
                                    dateTime = currentTime.toString();
    
                                    //Add x,y,z values to arrays for sensors
    
                                    if (sensorEvent.sensor.getType() == Sensor.TYPE_ACCELEROMETER && samples_acc < samples_total) 
                                    {
                                    accDoubleArrayX[samples_acc] = x;
                                    accDoubleArrayY[samples_acc] = y;
                                    accDoubleArrayZ[samples_acc] = z;
    
                                    samples_acc++;
                                    }
    
                                    if (sensorEvent.sensor.getType() == Sensor.TYPE_GYROSCOPE && samples_gyro < samples_total) 
                                    {
                                    gyroDoubleArrayX[samples_gyro] = x2;
                                    gyroDoubleArrayY[samples_gyro] = y2;
                                    gyroDoubleArrayZ[samples_gyro] = z2;
    
                                    samples_gyro++;
                                    }
    
                                    if (sensorEvent.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR && samples_rotation < samples_total) 
                                    {
                                    rotationDoubleArrayX[samples_rotation] = x3;
                                    rotationDoubleArrayY[samples_rotation] = y3;
                                    rotationDoubleArrayZ[samples_rotation] = z3;
                                    rotationDoubleArrayCos[samples_rotation] = cos3;
                                    rotationDoubleArrayAccuracy[samples_rotation] = accuracy3;
    
                                    samples_rotation++;
                                    }
    
                                }//end if flag=true
                                else 
                                {
                                //Log.d("", "samples < total but Flag is false - Data NOT inserted");
                                }
                }//end if at least one of arrays is not filled yet
    
                //all arrays have been filled   
                else 
                {
                    durationTime = System.currentTimeMillis() - startTime;
    
                    //Insert x,y,z values for sensors
                    dbRef.insertData(dateTime, accDoubleArrayX, accDoubleArrayY, accDoubleArrayZ,
                            gyroDoubleArrayX, gyroDoubleArrayY, gyroDoubleArrayZ,
                            rotationDoubleArrayX, rotationDoubleArrayY, rotationDoubleArrayZ, rotationDoubleArrayCos, rotationDoubleArrayAccuracy,
                            durationTime, label);
    
                    Log.d("", "Data inserted!");
                    Toast.makeText(this, "Data inserted!", Toast.LENGTH_LONG).show();
    
                    //Stop recording when given number of samples inserted
                    flag = false;
                }