2013-04-22 2 views
0

나는 여기서 무슨 일이 벌어지고 있는지 정말로 이해하지 못합니다. 나는 단지 가속도계 데이터를 수집하는 안드로이드 앱을 작성하고있다. 그것은 나에게 내가

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에서 내 개시 값입니다있어 오류이 오류

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; 
    } 
} 

다음입니다주고 계속하는 것은

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; 
+2

여기서 RCService.java의 행 번호 196은 무엇입니까? –

+0

두 개 이상의 객체에 대한 인스턴스화가 표시되지 않습니다. 'fOut' &'inChannel' – jnthnjns

+0

Acc 파일을 쓰기 시작하는 Writefile() 메소드 안에 있습니다 – Thawda

답변

0

나는 이유를 알고하지 않는 서비스를 확장 그것은 문제를 해결했습니다. 나는 공장을 공장 초기화했는데 지금은 완벽하게 잘 작동한다. 나는 이유를 모른다.

관련 문제