我真的不明白这里发生了什么。我只是写一个Android应用程序来收集Accelerometer数据。它一直给我这个错误
package com.thawda.mm;
public class RCService extends Service
{
private static final String LOGTAG2 = "RCService";
private boolean isTaking =false;
public void onCreate()
{
mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
tMgr =(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
Intent battery = this.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int level = battery.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = battery.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
batteryPct = level / (float)scale;
createFile();
myTimer();
Toast.makeText(this, "The Program Threat has created....", Toast.LENGTH_LONG).show();
Log.i(LOGTAG2,"ACC Service Running");
}
@Override
public void onDestroy()
{
super.onDestroy();
closeFile();
updateTimer.cancel();
mSensorManager.unregisterListener(mListener);
Toast.makeText(this, "Service Destroyed...", Toast.LENGTH_LONG).show();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
private SensorEventListener mListener = new SensorEventListener(){
public void onAccuracyChanged(Sensor sensor, int accuracy)
{
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event)
{
// TODO Auto-generated method stub
String data3;
//Date unixTime = new Date();
x=event.values[0];
y=event.values[1];
z=event.values[2];
// data3 = String.format(" %1$1.4f",
// x);
// Log.i(LOGTAG2,data3);
//TIME = event.timestamp;
// TIME2 = unixTime.getTime();
}
};
public void createFile()
{
String noTemp = tMgr.getDeviceId();
File rootPath=new File(Environment.getExternalStorageDirectory()+"/MM_ACC/");
String filepath =Environment.getExternalStorageDirectory()+"/MM_ACC/";
File myFile = new File(filepath);
String NAME = noTemp+"_"+DateFormat.format("yyyyMMdd-hhmmss", new Date())+"_MM_ACC_"+counter+".txt";
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
Toast.makeText(this, "Cannot use storage", Toast.LENGTH_SHORT).show();
//need to add about finish
Log.i(LOGTAG2,"Passed usage of external storage");
return;
}
//Create a new directory on External storage
if(!rootPath.exists())
{
rootPath.mkdirs();
}
if(!myFile.isDirectory())
myFile.mkdir();
myFile = new File(filepath+NAME);
try
{
myFile.createNewFile();
fOut = new FileOutputStream(myFile);
inChannel = fOut.getChannel();
myOutWriter=new OutputStreamWriter(fOut);
}
catch(IOException e)
{
e.printStackTrace();
}
}
public void WriteFile()
{
try
{
String data2;
Date unixTime = new Date();
TIME2 = unixTime.getTime();
data2 = String.format(", %1$1.4f, %2$1.4f, %3$1.4f",
x, y, z);
myOutWriter.write(TIME2+ data2 +"\r\n"); // fix the time
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e)
{
e.printStackTrace();
}
}
public void closeFile()
{
try
{
myOutWriter.close();
inChannel.close();
fOut.flush();
fOut.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void myTimer()
{
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
// IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
// registerReceiver(mReceiver, filter);
// mSensorManager.registerListener(mListener, mAccelerometer, SensorManager.SENSOR_DELAY_FASTEST); //(*need to fix for highest accuracy
updateTimer=new Timer("Update");
// startlocation();
updateTimer.scheduleAtFixedRate(new TimerTask()
{
public void run()
{
WriteFile();
try
{
//Log.d(LOGTAG2,"Timer 1 just ran");
filesize=inChannel.size();
if(filesize>100000)
{
counter++;
closeFile();
createFile();
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, 0, 100);
}
public boolean isTaking()
{
return isTaking;
}
}
以下是我得到的错误
AndroidRuntime(4708): java.lang.NullPointerException
AndroidRuntime(4708): at com.thawda.mm.RCService.WriteFile(RCService.java:196)
AndroidRuntime(4708): at com.thawda.mm.RCService$2.run(RCService.java:241)
AndroidRuntime(4708): at java.util.Timer$TimerImpl.run(Timer.java:284)
这些是我在公共类RCService extends Service
下的初始值SensorManager mSensorManager;
Sensor mAccelerometer, mField;
TelephonyManager tMgr;
FileOutputStream fOut;
OutputStreamWriter myOutWriter;
FileChannel inChannel;
long filesize =0;
long counter=0;
public long TIME2;
float x,y,z;
float Azimuth, Pitch, Roll;
public long TIME,TIME3;
private float[] mGravity;
private float[] mMagnetic;
public float batteryPct;
Timer updateTimer;
private static final String LOGTAG2 = "RCService";
private boolean isTaking =false;
答案 0 :(得分:0)
我不知道为什么它解决了这个问题。我工厂重置了我的手机,现在它工作得非常好。我不知道为什么。