我正在开发一个读取 GYRO 数据的 APP,然后将 X、Y、Z 值保存在存储在 SD 卡中的 excel 文件中。我正在使用 JXL 来实现 excel 写入,因为 excel 写入是一种资源消耗,我为它启动了新线程。我的代码编译正常,但是在手机上运行时出现错误,如下:
06-20 17:15:45.328: W/dalvikvm(19897): threadid=13: thread exiting with uncaught exception (group=0x41c30450)
06-20 17:15:45.328: E/AndroidRuntime(19897): FATAL EXCEPTION: Thread-58127
06-20 17:15:45.328: E/AndroidRuntime(19897): java.lang.NullPointerException
06-20 17:15:45.328: E/AndroidRuntime(19897): at hunk.hong.sonymobile.gyrotest.MainActivity$2.run(MainActivity.java:169)
06-20 17:15:45.328: E/AndroidRuntime(19897): at java.lang.Thread.run(Thread.java:856)
以下是我的源代码:
public class MainActivity extends Activity implements SensorEventListener {
private TextView TV;
private TextView TV1;
private Button button1;
private SensorManager mSensorManager;
private Sensor mSensor;
private EditText mEditText;
private SensorEvent mSensorEvent;
private Thread mThread;
private SensorEvent event;
DatabaseHandler db = new DatabaseHandler(this);
Timer timer = new Timer();
Handler handler = new Handler(){
public void handleMessage(Message msg){
switch (msg.what){
case 1:
//do someting
//setTitle("hear me?");
break;
}
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.button1);
mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
TV = (TextView)findViewById(R.id.action_settings);
TV1 = (TextView)findViewById(R.id.textView1);
mEditText = (EditText)findViewById(R.id.editText1);
if (mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) == null){
//sorry, there is no GYRO in your device
TV.setText("sorry, there is no GYRO in your device!!!");
}
else{
mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
TV.setText("vendor:"+mSensor.getName()+"\n"+"version:"+mSensor.getVersion()+"\n"+"MAX Range:"+mSensor.getMaximumRange()
+"\n"+"Resulation:"+mSensor.getResolution());
}
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
if(mThread == null){
mThread = new Thread(runnable);
mThread.start();//thread start
}
else{
//Toast.makeText(this, "thread is already running", Toast.LENGTH_LONG).show();
}
}
});
}
Runnable runnable = new Runnable(){
@Override
public void run(){
String Fnamexls = "GyroTest"+".xls";
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard.getAbsolutePath()+"/GyroTest");
directory.mkdir();
File file = new File(directory,Fnamexls);
WritableWorkbook workbook;
try{
int i=1;
workbook = Workbook.createWorkbook(file);
WritableSheet sheet = workbook.createSheet("First Sheet", 0);
Label labelTitle1 = new Label(0,0,"X-rotation(R/S)");
Label labelTitle2 = new Label(1,0,"Y-rotation(R/S)");
Label labelTitle3 = new Label(2,0,"Z-rotation(R/S)");
try{//write title
sheet.addCell(labelTitle1);
sheet.addCell(labelTitle2);
sheet.addCell(labelTitle3);
}catch(RowsExceededException e){
e.printStackTrace();
}catch (WriteException e){
e.printStackTrace();
}
for (i=1;i<=100;i++){//write 100 data in excel
Label labeli0 = new Label(0,i,Float.toString(event.values[0]));
Label labeli1 = new Label(1,i,Float.toString(event.values[1]));
Label labeli2 = new Label(2,i,Float.toString(event.values[2]));
try{
sheet.addCell(labeli0);
sheet.addCell(labeli1);
sheet.addCell(labeli2);
}catch (RowsExceededException e){
e.printStackTrace();
}catch (WriteException e){
e.printStackTrace();
}
}
workbook.write();
try{
workbook.close();
}catch(WriteException e){
e.printStackTrace();
}
}catch (IOException e){
e.printStackTrace();
}
}
};
protected void onResume()
{
super.onResume();
/*register the sensor listener to listen to the gyroscope sensor, use the
call backs defined in this class, and gather the sensor information as quick
as possible*/
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE),SensorManager.SENSOR_DELAY_UI); //get sensor readout suitable for user interface
}
//When this Activity isn't visible anymore
@Override
protected void onStop()
{
//unregister the sensor listener when application is not visible
mSensorManager.unregisterListener(this);
super.onStop();
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1)
{
//Do nothing.
}
@Override
public void onSensorChanged(SensorEvent event)
{
//if sensor is unreliable, return void
if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
{
return;
}
//else it will output the rotation values
TV1.setText("Rotation X :"+ Float.toString(event.values[0]) +"\n"+
"Rotation Y :"+ Float.toString(event.values[1]) +"\n"+
"Rotation Z :"+ Float.toString(event.values[2]));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
对于错误消息,问题应该在下面一行:
Label labeli0 = new Label(0,i,Float.toString(event.values[0]));
Label labeli1 = new Label(1,i,Float.toString(event.values[1]));
Label labeli2 = new Label(2,i,Float.toString(event.values[2]));
任何人都可以帮我找到错误吗?以及如何解决这个问题?我如何在新线程中读取陀螺仪数据?