service总是不能初始化,注释掉一部分程序后可以初始化,咋回事?求解释

service总是不能初始化,注释掉一部分程序后可以初始化,怎么回事?求解释。
本帖最后由 c905452934c 于 2015-01-22 12:56:11 编辑
这里是已注释掉的程序,可以初始化,不会报错,但是只要把注释去掉就不能初始化service,怎么回事?
package org.text.welcome;

import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.Gravity;
import android.widget.Toast;

public class SerchService extends Service 
{
private final static String whichtype02 = "0902x";//处理位置信息

private LocationManager locmanager;

// final SharedPreferences near_account_sharedRead= getSharedPreferences("near_account_Data", 
//         Activity.MODE_PRIVATE);

@Override
public IBinder onBind(Intent arg0) 
{
return null;
}

@Override
public void onStart(Intent intent, int startId) 
{
System.out.println("服务开启");

super.onStart(intent, startId);

System.out.println("未获取位置信息01");

locmanager = (LocationManager) getSystemService(
         Context.LOCATION_SERVICE);

System.out.println("未获取位置信息02");
        
        final Location loc = locmanager.getLastKnownLocation(
         LocationManager.GPS_PROVIDER);  
        
        System.out.println("已获取位置信息");
        
        SharedPreferences shared = getSharedPreferences("tempAccountData", 0);  
        final String nowName = shared.getString("account_name", "");        
        
        StringBuilder locationstr01 = new StringBuilder();
        locationstr01.append(whichtype02);
        locationstr01.append(",");
        locationstr01.append(nowName);
        System.out.println(nowName);
        locationstr01.append(",");
        locationstr01.append(loc.getLongitude());
        System.out.println(loc.getLongitude());
        locationstr01.append(",");
        locationstr01.append(loc.getLatitude());
        
        Handler handler = new Handler()
{      
@Override
public void handleMessage(Message msg) 
{
if(msg.what==0x123)
{
// String[] temp_str = msg.obj.toString().split(",");
//
// SharedPreferences.Editor editor = near_account_sharedRead.edit(); 
// editor.clear();
// editor.commit();
//
// for (int i=0;i<temp_str.length;i++)
// {
// editor.putString("account_name"+i, temp_str[i]);
// }
//
// editor.commit();

}
}
};

new Thread(new SendLocationThread(locationstr01,handler)).start();        
        
        locmanager.requestLocationUpdates(
         LocationManager.GPS_PROVIDER,5000,8,new LocationListener()
         {
public void onLocationChanged(Location location) 
{
StringBuilder locationstr = new StringBuilder();
locationstr.append(whichtype02);
        locationstr.append(",");
        locationstr.append(nowName);         
        locationstr.append(",");
locationstr.append(location.getLongitude());
System.out.println(location.getLongitude());
locationstr.append(",");
locationstr.append(location.getLatitude());

Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg) 
{
if(msg.what==0x123)
{
// String[] temp_str = msg.obj.toString().split(",");
//
// SharedPreferences.Editor editor = near_account_sharedRead.edit();
// editor.clear();
// editor.commit();
//
// for (int i=0;i<temp_str.length;i++)
// {
// editor.putString("account_name", temp_str[i]);
// }
//
// editor.commit();

}
}
};

new Thread(new SendLocationThread(locationstr,handler)).start();
}

public void onProviderDisabled(String provider) 
{
Toast toast = Toast.makeText(getApplicationContext(),
     "定位设备不可用", Toast.LENGTH_LONG);
   toast.setGravity(Gravity.TOP, 0, 0);
   toast.show();
}

public void onProviderEnabled(String provider) 
{
Location location=locmanager.getLastKnownLocation(provider);

StringBuilder locationstr = new StringBuilder();
locationstr.append(whichtype02);
        locationstr.append(",");
        locationstr.append(nowName);         
        locationstr.append(",");
locationstr.append(location.getLongitude());
locationstr.append(",");
locationstr.append(location.getLatitude());

Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg) 
{
if(msg.what==0x123)
{
// String[] temp_str = msg.obj.toString().split(",");
//
// SharedPreferences.Editor editor = near_account_sharedRead.edit();
// editor.clear();
// editor.commit();
//
// for (int i=0;i<temp_str.length;i++)
// {
// editor.putString("account_name", temp_str[i]);
// }
//
// editor.commit();

}
}
};

new Thread(new SendLocationThread(locationstr,handler)).start();
}

public void onStatusChanged(String provider, int status,
Bundle extras) 
{

}
        
         }); 
}
}

------解决思路----------------------
getSharedPreferences("near_account_Data", Activity.MODE_PRIVATE);
需要在service开始执行oncreate之后才能使用。
和this相关的方法都要这样