开始服务时强制关闭

时间:2011-12-13 22:32:57

标签: android eclipse service android-service

我有一项活动,我希望从服务中更新它。当我尝试启动服务时,我收到一个强制关闭错误。

logcat的

12-13 15:26:24.574: E/AndroidRuntime(13741): FATAL EXCEPTION: main
12-13 15:26:24.574: E/AndroidRuntime(13741): java.lang.RuntimeException: Unable to instantiate service www.freshapp.com.wherewhen.html.SetTime: java.lang.NullPointerException
12-13 15:26:24.574: E/AndroidRuntime(13741):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2005)
12-13 15:26:24.574: E/AndroidRuntime(13741):    at android.app.ActivityThread.access$2500(ActivityThread.java:124)
12-13 15:26:24.574: E/AndroidRuntime(13741):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1022)
12-13 15:26:24.574: E/AndroidRuntime(13741):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-13 15:26:24.574: E/AndroidRuntime(13741):    at android.os.Looper.loop(Looper.java:123)
12-13 15:26:24.574: E/AndroidRuntime(13741):    at android.app.ActivityThread.main(ActivityThread.java:3806)
12-13 15:26:24.574: E/AndroidRuntime(13741):    at java.lang.reflect.Method.invokeNative(Native Method)
12-13 15:26:24.574: E/AndroidRuntime(13741):    at java.lang.reflect.Method.invoke(Method.java:507)
12-13 15:26:24.574: E/AndroidRuntime(13741):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-13 15:26:24.574: E/AndroidRuntime(13741):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-13 15:26:24.574: E/AndroidRuntime(13741):    at dalvik.system.NativeStart.main(Native Method)
12-13 15:26:24.574: E/AndroidRuntime(13741): Caused by: java.lang.NullPointerException
12-13 15:26:24.574: E/AndroidRuntime(13741):    at www.freshapp.com.wherewhen.html.SetTime.<init>(SetTime.java:21)
12-13 15:26:24.574: E/AndroidRuntime(13741):    at java.lang.Class.newInstanceImpl(Native Method)
12-13 15:26:24.574: E/AndroidRuntime(13741):    at java.lang.Class.newInstance(Class.java:1409)
12-13 15:26:24.574: E/AndroidRuntime(13741):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2002)

服务类

public class SetTime extends Service {


private Handler hanl;
private Context context;
LayoutInflater inflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);
View name = inflater.inflate(R.id.editName, null);

View lon = inflater.inflate(R.id.editName, null);

View lat = inflater.inflate(R.id.editName, null);

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate(){

}

 @Override
  public int onStartCommand(Intent intent, int flags, int startId) {

     final LocationManager mlocManager = 
                (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        final LocationListener mlocListener = new LocationHelper();
      Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();

        final Runnable r = new Runnable(){
      public void run(){
            mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
                    mlocListener);

            ((TextView) lat).setText(""+ LocationHelper.getLatitude());
            ((TextView) lon).setText("" + LocationHelper.getLongitude());


             hanl.postDelayed(this, 1000);


             //String date = s.format(new Date());
                String mName = ((EditText) name).getText().toString();
                 String mLat = ((EditText) lat).getText().toString();
                 String mLon = ((EditText) lon).getText().toString();




        }
            };
            hanl.postDelayed(r, 1000);


      // If we get killed, after returning from here, restart
      return START_STICKY;
  }

}

第21行

LayoutInflater inflater = (LayoutInflater)context.getSystemService(LAYOUT_INFLATER_SERVICE);

1 个答案:

答案 0 :(得分:0)

您正尝试在静态初始值设定项中创建LayoutInflater。这不行。从战术上讲,它不起作用,因为您试图在getSystemService()上调用context,此时nullService

此外,LayoutInflaterEditText没有用处,因此您应该删除第21行以及引用它的所有内容,例如您无法使用的所有Service小部件。或者,将此Activity转换为Activity,因为Service具有用户界面,而{{1}}则没有。