问题描述
importjava.text.DecimalFormat;importandroid.os.Bundle;importandroid.app.Activity;importandroid.content.Intent;importandroid.hardware.Sensor;importandroid.hardware.SensorEvent;importandroid.hardware.SensorEventListener;importandroid.hardware.SensorManager;importandroid.text.StaticLayout;importandroid.view.Menu;importandroid.view.View;importandroid.view.View.OnClickListener;importandroid.widget.Button;importandroid.widget.EditText;importandroid.widget.Toast;publicclassActivity2extendsActivity{privateButtonbuttonx=null;privateButtonbuttony=null;privateButtonbuttonz=null;privateButtonbuttonxy=null;privateButtonbuttonxz=null;privateButtonbuttonyz=null;privateButtonbuttonxyz=null;privateButtonbutton=null;privateEditTextedit=null;privatefloatvalues[];privateSensorManagermySensorManager=null;privateSensormyAccelerometer;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.main2);buttonx=(Button)this.findViewById(R.id.buttonx);buttony=(Button)this.findViewById(R.id.buttony);buttonz=(Button)this.findViewById(R.id.buttonz);buttonxy=(Button)this.findViewById(R.id.buttonxy);buttonxz=(Button)this.findViewById(R.id.buttonxz);buttonyz=(Button)this.findViewById(R.id.buttonyz);buttonxyz=(Button)this.findViewById(R.id.buttonxyz);button=(Button)this.findViewById(R.id.button);edit=(EditText)this.findViewById(R.id.edit);mySensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);myAccelerometer=mySensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);buttonx.setOnClickListener(newOnClickListener(){@OverridepublicvoidonClick(Viewv){//TODOAuto-generatedmethodstubIntentintent=newIntent();intent.setClass(Activity2.this,Activity2x.class);startActivity(intent);}});}publicvoidonResume(){super.onResume();mySensorManager.registerListener(mySensorListener,myAccelerometer,SensorManager.SENSOR_DELAY_NORMAL);}protectedvoidonPause(){super.onPause();mySensorManager.unregisterListener(mySensorListener);}privateSensorEventListenermySensorListener=newSensorEventListener(){@OverridepublicvoidonSensorChanged(SensorEventevent){//TODOAuto-generatedmethodstubvalues=event.values;Doubledz=values[2]-9.81;DecimalFormatdf=newDecimalFormat("0.00");Doubledxy=Math.sqrt(values[0]*values[0]+values[1]*values[1]);Doubledxz=Math.sqrt(values[0]*values[0]+dz*dz);Doubledyz=Math.sqrt(values[1]*values[1]+dz*dz);Doubledxyz=Math.sqrt(values[0]*values[0]+values[1]*values[1]+dz*dz);buttonx.setText("X轴方向上的加速度="+df.format(values[0]));buttony.setText("Y轴方向上的加速度="+df.format(values[1]));buttonz.setText("Z轴方向上的加速度="+df.format(dz));buttonxy.setText("X与Y方向上的和加速="+df.format(dxy));buttonxz.setText("X与Z方向上的和加速="+df.format(dxz));buttonyz.setText("Y与Z方向上的和加速="+df.format(dyz));buttonxyz.setText("合加速度="+df.format(dxyz));}@OverridepublicvoidonAccuracyChanged(Sensorsensor,intaccuracy){//TODOAuto-generatedmethodstub}};publicvoidonRun(){Stringstr=edit.getText().toString().trim();if(str!=null){Doublet1=Double.parseDouble(str);longt=(long)(t1*1000);try{Thread.sleep(t);}catch(InterruptedExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}}
解决方案
解决方案二:
edit.getText().toString().trim();你这个分别判断下.edit是不是nullgetText方法返回的是不是nul得一个个判断.
解决方案三:
先用TextUtil(edit.getText())判断是不是空,不空才能.toString().trim()吧