嗨,我目前正在做我的最后一年项目。为此,我需要“将传感器的连续数据从 textviews 记录到 .xls 表中”。为此,我正在使用 Java 的 Apache POI 库,它允许您创建 excel 工作簿和工作表等。问题是我的代码能够成功创建工作表。但是当我尝试记录加速度计传感器的变化值(让加速度计仅用于讨论)并将这些值写入文件时,问题就来了,点击按钮。这是我来自 android studio 项目的 .java 文件的代码。
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Workbook;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity implements SensorEventListener, View.OnClickListener {
public static int i = 1;
public static boolean a = false;
public static String filename;
private SensorManager sensorManager;
Sensor accelerometer, gyroscope, magnetometer;
TextView ax, ay, az, gx, gy, gz, mx, my, mz;
Button btnStart, btnStop;
Workbook workbook;
//ArrayList<String> array = new ArrayList<>();
FileOutputStream fos = null;
HSSFSheet sheet;
Row row;
int rownum = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ax = findViewById(R.id.valueX);
ay = findViewById(R.id.valueY);
az = findViewById(R.id.valueZ);
gx = findViewById(R.id.gvalueX);
gy = findViewById(R.id.gvalueY);
gz = findViewById(R.id.gvalueZ);
mx = findViewById(R.id.mvalueX);
my = findViewById(R.id.mvalueY);
mz = findViewById(R.id.mvalueZ);
//---------------------------------------------------------------------------------------------------//
btnStart = findViewById(R.id.btnStart);
btnStop = findViewById(R.id.btnStop);
btnStart.setOnClickListener(this);
btnStop.setOnClickListener(this);
filename = "File.xls";
//This is the file, that will be made workbook when doing "workbook.write(fos{filename..})"
workbook = new HSSFWorkbook(); //creates workbook only once when program is started
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
assert sensorManager != null;
accelerometer = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
gyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
magnetometer = sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
registerListeners(); //a Function.
}
private void registerListeners(){
//Registering the listeners to start recording data
if (accelerometer != null) {
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
} else {
Toast.makeText(this, "Accelerometer is not supported", Toast.LENGTH_SHORT).show();
}
if (gyroscope != null) {
sensorManager.registerListener(this, gyroscope, SensorManager.SENSOR_DELAY_NORMAL);
} else {
Toast.makeText(this, "Gyroscope is not supported", Toast.LENGTH_SHORT).show();
}
if (magnetometer != null) {
sensorManager.registerListener(this, magnetometer, SensorManager.SENSOR_DELAY_NORMAL);
} else {
Toast.makeText(this, "Magnetometer is not supported", Toast.LENGTH_SHORT).show();
}
}
//THIS IS THE PORTION WITH ISSUE .-.
@Override
public void onSensorChanged(SensorEvent sensorEvent) {
Sensor sensor = sensorEvent.sensor;
if (sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
ax.setText(String.valueOf(sensorEvent.values[0]));
ay.setText(String.valueOf(sensorEvent.values[1]));
az.setText(String.valueOf(sensorEvent.values[2]));
}
else if (sensor.getType() == Sensor.TYPE_GYROSCOPE) {
gx.setText(String.valueOf(sensorEvent.values[0]));
gy.setText(String.valueOf(sensorEvent.values[1]));
gz.setText(String.valueOf(sensorEvent.values[2]));
}
else if (sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {
mx.setText(String.valueOf(sensorEvent.values[0]));
my.setText(String.valueOf(sensorEvent.values[1]));
mz.setText(String.valueOf(sensorEvent.values[2]));
//Here i try to record sensor values... BUT it fails......!!!!!!!!!!!!!
if(a) {
if(row!=null) {
row = sheet.createRow(rownum); //rownum =3 (initially)
//picking accelerometer values only.
row.createCell(0).setCellValue(ax.getText().toString());
row.createCell(1).setCellValue(ay.getText().toString());
row.createCell(2).setCellValue(az.getText().toString());
}
try {
fos = openFileOutput(filename, MODE_APPEND);
((HSSFWorkbook) workbook).write(fos);
Toast.makeText(this, "Done" , Toast.LENGTH_SHORT).show();
}
catch (IOException e) {
e.printStackTrace();
}
rownum++;
}
}
}
public void onAccuracyChanged(Sensor sensor, int i) {
//leave it empty for now
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btnStart:
if(createSheet()){
a = true;
}
else Toast.makeText(this, "Sheet could not be created", Toast.LENGTH_SHORT).show();
//i++;
break;
case R.id.btnStop:
a = false;
//i++;
Toast.makeText(this, "Values Recorded Successfully", Toast.LENGTH_LONG).show();
/*try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
} */
break;
}
}
private boolean createSheet() {
try{
Toast.makeText(this, " " + i, Toast.LENGTH_SHORT).show();
sheet = ((HSSFWorkbook) workbook).createSheet("Sheet_" + i);
//Toast.makeText(this, "Sheet " + i, Toast.LENGTH_SHORT).show();
Cell cell;
row = sheet.createRow(0);
cell = row.createCell(0);
cell.setCellValue("Mobile Phone Sensors Readings");
//row.createCell(0).setCellValue("Mobile Phone Sensors Readings");
row = sheet.createRow(1);
row.createCell(0).setCellValue("Accelerometer");
row.createCell(3).setCellValue("Gyroscope");
row.createCell(6).setCellValue("Magnetometer");
row = sheet.createRow(2);
row.createCell(0).setCellValue("X-Axis");
row.createCell(1).setCellValue("Y-axis");
row.createCell(2).setCellValue("Z-axis");
row.createCell(3).setCellValue("X-Axis");
row.createCell(4).setCellValue("Y-axis");
row.createCell(5).setCellValue("Z-axis");
row.createCell(6).setCellValue("X-Axis");
row.createCell(7).setCellValue("Y-axis");
row.createCell(8).setCellValue("Z-axis");
try {
fos = openFileOutput(filename, MODE_APPEND);
((HSSFWorkbook) workbook).write(fos);
}
catch (IOException e) {
e.printStackTrace();
return false;
}
} //try1 ends.
catch (Exception ex){
ex.printStackTrace();
return false;
} //catch for try1 ends.
return true;
} //createSheet function ends.
}
有人可以帮忙吗?问题区域在 onSensorValuesChanged() 函数中。
谢谢是提前。