我希望我的加速度计在一定的时间间隔内运行,所以我用线程来做。但现在我遇到了一个问题,即有时候我的应用程序崩溃,没有点击任何东西而只是崩溃。例如,如果我将间隔设置为5秒,前5秒工作,但在第10秒,它将崩溃。非常感谢您的帮助。
主页
package com.example.suntracking;
import java.util.Calendar;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;
public class AccelerometerMain extends Activity{
TextView showax,showay,showaxold,showayold,showaxdiff,showaydiff,showsx,showsy,showlatnew,showlongnew,showdate,showtime,showday;;
int timesec,timeminute,timehour,dateday,datemonth,dateyear,day,ampm;
String daytext,ampmtext,showazi,showele,shownoday,showra,showdecli,showh_angle,shows_time_hour,shows_time_minute,shows_time_sec,
sshowday,takeampmtext;
int timeval,taketimesec,taketimeminute,taketimehour,takedateday,takedatemonth,takedateyear;
int samintval;
float takeax,takeay,takeaxold,takeayold,takeaxdiff,takeaydiff,takesx,takesy,takelatnew,takelongnew;
Handler mHandler = new Handler();
Thread t;
volatile boolean shutdown = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.accelerometer);
initialize();
loadSavedPreferences();
t = new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
while (!shutdown) {
try {
Thread.sleep(samintval);
mHandler.post(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// Write your code here to update the UI.
Save_fetchPref();
run_accelerometer();
run_cal();
}
private void run_accelerometer() {
// TODO Auto-generated method stub
Intent j=new Intent(AccelerometerMain.this,Accelerometer.class);
startActivity(j);
savePreferences();
loadSavedpreferences();
}
private void run_cal() {
// TODO Auto-generated method stub
Intent j=new Intent(AccelerometerMain.this,Calculation.class);
startActivity(j);
savePreferences();
loadSavedpreferences();
}
private void Save_fetchPref() {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
Calendar c = Calendar.getInstance();
timesec = c.get(Calendar.SECOND);
timeminute = c.get(Calendar.MINUTE);
timehour = c.get(Calendar.HOUR_OF_DAY);
ampm = c.get(Calendar.AM_PM);
if(ampm==0){
ampmtext="AM";
}else {
ampmtext="PM";
}
dateday = c.get(Calendar.DAY_OF_MONTH);
datemonth = c.get(Calendar.MONTH);
datemonth += 1;
dateyear = c.get(Calendar.YEAR);
day = c.get(Calendar.DAY_OF_WEEK);
switch(day){
case 1:
daytext="SUNDAY";
break;
case 2:
daytext="MONDAY";
break;
case 3:
daytext="TUESDAY";
break;
case 4:
daytext="WEDNESDAY";
break;
case 5:
daytext="THURSDAY";
break;
case 6:
daytext="FRIDAY";
break;
case 7:
daytext="SATURDAY";
break;
}
savePreferences();
loadSavedpreferences();
}
private void loadSavedpreferences() {
// TODO Auto-generated method stub
SharedPreferences savedata= getSharedPreferences("savealldata",0);
sshowday=savedata.getString("daytext",null);
showday.setText(sshowday);
takedateday=savedata.getInt("dateday",00);
takedatemonth=savedata.getInt("datemonth", 00);
takedateyear=savedata.getInt("dateyear", 00);
showdate.setText(takedateday + "/" + takedatemonth + "/" + takedateyear);
taketimesec=savedata.getInt("timesec", 00);
taketimeminute=savedata.getInt("timeminute", 00);
taketimehour=savedata.getInt("timehour", 00);
takeampmtext=savedata.getString("ampmtext", "AM");
showtime.setText(taketimehour + ":" + taketimeminute + ":" + taketimesec + takeampmtext);
takeax=savedata.getFloat("ax", 0);
takeay=savedata.getFloat("ay", 0);
takeaxold=savedata.getFloat("axtemp", 0);
takeayold=savedata.getFloat("aytemp", 0);
takeaxdiff=savedata.getFloat("axdiff", 0);
takeaydiff=savedata.getFloat("aydiff", 0);
takesx=savedata.getFloat("sx", 0);
takesy=savedata.getFloat("sy", 0);
takelatnew=savedata.getFloat("latval", 0);
takelongnew=savedata.getFloat("longval", 0);
showax.setText("x-acceleration :" + takeax);
showay.setText("y-acceleration :" + takeay);
showaxold.setText("previous x-acceleration : " + takeaxold);
showayold.setText("previous y-acceleration : " + takeayold);
showaxdiff.setText("x-acceleration difference : " + takeaxdiff);
showaydiff.setText("y-acceleration difference :" + takeaydiff);
showsx.setText("x-displacement :" + takesx);
showsy.setText("y-displacement :" + takesy);
showlatnew.setText("latitude :" + takelatnew);
showlongnew.setText("longitude :" + takelongnew);
}
private void savePreferences() {
// TODO Auto-generated method stub
SharedPreferences savedata = getSharedPreferences("savealldata",0);
Editor editor=savedata.edit();
editor.putInt("timesec",timesec);
editor.putInt("timeminute", timeminute);
editor.putInt("timehour", timehour);
editor.putInt("dateday", dateday);
editor.putInt("datemonth", datemonth);
editor.putInt("dateyear", dateyear);
editor.putInt("ampm", ampm);
editor.putString("daytext", daytext);
editor.putString("ampmtext", ampmtext);
editor.commit();
}
});
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
});
t.start();
}
private void loadSavedPreferences() {
// TODO Auto-generated method stub
SharedPreferences savedata= getSharedPreferences("savealldata",0);
samintval = savedata.getInt("samintval", 1000);
}
private void initialize() {
// TODO Auto-generated method stub
showday=(TextView)findViewById(R.id.tvshowdayaccele);
showdate=(TextView)findViewById(R.id.tvshowdateaccele);
showtime=(TextView)findViewById(R.id.tvshowtimeaccele);
showax=(TextView)findViewById(R.id.tvaxnew);
showay=(TextView)findViewById(R.id.tvaynew);
showaxold=(TextView)findViewById(R.id.tvaxold);
showayold=(TextView)findViewById(R.id.tvayold);
showaxdiff=(TextView)findViewById(R.id.tvaxdiff);
showaydiff=(TextView)findViewById(R.id.tvaydiff);
showsx=(TextView)findViewById(R.id.tvsx);
showsy=(TextView)findViewById(R.id.tvsy);
showlatnew=(TextView)findViewById(R.id.tvlatnew);
showlongnew=(TextView)findViewById(R.id.tvlongnew);
}
}
加速度计
package com.example.suntracking;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;
public class Accelerometer extends Activity implements SensorEventListener{
Sensor accelerometer;
SensorManager sm;
int samintval;
float ax,ay,axtemp ,aytemp ,sx,sy,axdiff,aydiff,latnew,latold,longnew,longold;
TextView showax,showay,showaxtemp,showaytemp;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
loadsavedPref();
initialize();
sm.registerListener(this, accelerometer,sm .SENSOR_DELAY_UI);
savePreferences();
finish();
}
@Override
protected void onResume()
{
super.onResume();
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
sm.unregisterListener(this);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
sm.unregisterListener(this);
}
private void initialize() {
// TODO Auto-generated method stub
sm=(SensorManager)getSystemService(SENSOR_SERVICE);
accelerometer=sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
private void loadsavedPref() {
// TODO Auto-generated method stub
SharedPreferences savedata= getSharedPreferences("savealldata",0);
samintval = savedata.getInt("samintval", 1000);
latold = savedata.getInt("latval", 0);
longold = savedata.getInt("longval", 0);
}
private void cal_displace() {
// TODO Auto-generated method stub
axdiff= ax - axtemp;
aydiff = ay - aytemp;
sx = (float) (0.5 * ((axdiff)/(samintval/1000)) * (samintval / 1000)*(samintval / 1000));
sy = (float) (0.5 * ((aydiff)/(samintval/1000)) * (samintval / 1000)*(samintval / 1000));
latnew = latold + (sy / 111229);
longnew = (float) (longold + (sx / 71695.8));
axtemp = ax;
aytemp = ay;
}
@Override
public void onAccuracyChanged(Sensor arg0, int arg1) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)
return;
ax = event.values[0];
ay = event.values[1];
cal_displace();
}
private void savePreferences() {
// TODO Auto-generated method stub
SharedPreferences savedata = getSharedPreferences("savealldata",0);
Editor editor=savedata.edit();
editor.putFloat("ax", ax);
editor.putFloat("ay", ay);
editor.putFloat("axtemp", axtemp);
editor.putFloat("aytemp", aytemp);
editor.putFloat("axdiff", axdiff);
editor.putFloat("aydiff", aydiff);
editor.putFloat("sx", sx);
editor.putFloat("sy", sy);
editor.putFloat("latval", latnew);
editor.putFloat("longval", longnew);
editor.commit();
}
}
logcat的
02-07 04:52:44.632: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.632: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.632: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.632: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.632: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.632: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.632: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.632: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.632: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.632: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.632: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.632: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.632: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.632: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.632: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.632: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.632: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.642: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.642: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.642: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.642: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.642: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.642: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.652: D/libEGL(2298): loaded /vendor/lib/egl/libEGL_adreno.so
02-07 04:52:44.652: D/libEGL(2298): loaded /vendor/lib/egl/libGLESv1_CM_adreno.so
02-07 04:52:44.652: D/libEGL(2298): loaded /vendor/lib/egl/libGLESv2_adreno.so
02-07 04:52:44.652: I/Adreno-EGL(2298): <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build: RVADDULA_AU_LINUX_ANDROID_JB_3.1.2.04.03.00.125.097+PATCH[ES]_msm8974_JB_3.1.2_CL3905453_release_ENGG (CL3905453)
02-07 04:52:44.652: I/Adreno-EGL(2298): OpenGL ES Shader Compiler Version: 17.01.10.SPL
02-07 04:52:44.652: I/Adreno-EGL(2298): Build Date: 09/12/13 Thu
02-07 04:52:44.652: I/Adreno-EGL(2298): Local Branch: ss-h-project-sba-final
02-07 04:52:44.652: I/Adreno-EGL(2298): Remote Branch: quic/jb_3.1
02-07 04:52:44.652: I/Adreno-EGL(2298): Local Patches: 8963a7534e7db6c9821a277d9be96c4cf0af87fa Do not clear the color components unless mask set. This disable clearOnResolve when color mask is not set.
02-07 04:52:44.652: I/Adreno-EGL(2298): 0558bffe422425e3a63d0802691301942f57995d Add the stencil
02-07 04:52:44.692: D/OpenGLRenderer(2298): Enabling debug mode 0
02-07 04:52:44.702: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.702: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.712: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.712: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.712: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.712: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.712: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.712: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.712: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.712: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.712: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.712: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.712: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.712: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.712: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.712: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.712: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.712: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.712: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.712: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.712: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.712: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:44.722: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:48.892: W/System.err(2298): java.lang.InterruptedException
02-07 04:52:48.892: W/System.err(2298): at java.lang.VMThread.sleep(Native Method)
02-07 04:52:48.892: W/System.err(2298): at java.lang.Thread.sleep(Thread.java:1013)
02-07 04:52:48.892: W/System.err(2298): at java.lang.Thread.sleep(Thread.java:995)
02-07 04:52:48.892: W/System.err(2298): at com.example.suntracking.MainActivity$1.run(MainActivity.java:60)
02-07 04:52:48.892: W/System.err(2298): at java.lang.Thread.run(Thread.java:841)
02-07 04:52:48.982: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:48.982: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:49.022: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:49.022: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:53.992: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:53.992: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:53.992: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:54.002: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:54.002: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:54.002: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:54.012: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:54.012: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:54.012: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:54.022: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:54.022: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:54.092: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:54.092: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:54.092: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:54.102: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:54.102: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:54.102: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:54.102: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:54.102: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:54.102: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:54.102: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:54.102: D/TextLayoutCache(2298): Enable myanmar Zawgyi converter
02-07 04:52:54.242: D/AndroidRuntime(2298): Shutting down VM
02-07 04:52:54.242: W/dalvikvm(2298): threadid=1: thread exiting with uncaught exception (group=0x41853898)
02-07 04:52:54.252: E/AndroidRuntime(2298): FATAL EXCEPTION: main
02-07 04:52:54.252: E/AndroidRuntime(2298): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.suntracking/com.example.suntracking.Accelerometer}: java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.Integer
02-07 04:52:54.252: E/AndroidRuntime(2298): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2294)
02-07 04:52:54.252: E/AndroidRuntime(2298): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2348)
02-07 04:52:54.252: E/AndroidRuntime(2298): at android.app.ActivityThread.access$700(ActivityThread.java:159)
02-07 04:52:54.252: E/AndroidRuntime(2298): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
02-07 04:52:54.252: E/AndroidRuntime(2298): at android.os.Handler.dispatchMessage(Handler.java:99)
02-07 04:52:54.252: E/AndroidRuntime(2298): at android.os.Looper.loop(Looper.java:137)
02-07 04:52:54.252: E/AndroidRuntime(2298): at android.app.ActivityThread.main(ActivityThread.java:5414)
02-07 04:52:54.252: E/AndroidRuntime(2298): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 04:52:54.252: E/AndroidRuntime(2298): at java.lang.reflect.Method.invoke(Method.java:525)
02-07 04:52:54.252: E/AndroidRuntime(2298): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
02-07 04:52:54.252: E/AndroidRuntime(2298): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
02-07 04:52:54.252: E/AndroidRuntime(2298): at dalvik.system.NativeStart.main(Native Method)
02-07 04:52:54.252: E/AndroidRuntime(2298): Caused by: java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.Integer
02-07 04:52:54.252: E/AndroidRuntime(2298): at android.app.SharedPreferencesImpl.getInt(SharedPreferencesImpl.java:240)
02-07 04:52:54.252: E/AndroidRuntime(2298): at com.example.suntracking.Accelerometer.loadsavedPref(Accelerometer.java:62)
02-07 04:52:54.252: E/AndroidRuntime(2298): at com.example.suntracking.Accelerometer.onCreate(Accelerometer.java:25)
02-07 04:52:54.252: E/AndroidRuntime(2298): at android.app.Activity.performCreate(Activity.java:5372)
02-07 04:52:54.252: E/AndroidRuntime(2298): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
02-07 04:52:54.252: E/AndroidRuntime(2298): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
02-07 04:52:54.252: E/AndroidRuntime(2298): ... 11 more