启动服务时强制关闭

发布于 2024-12-21 05:12:18 字数 3932 浏览 1 评论 0原文

我有一项活动,我希望通过服务对其进行更新。当我尝试启动服务时,出现强制关闭错误。

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

}

LINE 21

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

I have an activity and I want it to be updated from a service. when I try to start the service I get a force close error.

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)

SERVICE CLASS

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

}

LINE 21

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挥剑断情 2024-12-28 05:12:18

您正在尝试在静态初始值设定项中创建 LayoutInflater。这是行不通的。从战术上讲,它不会起作用,因为您尝试在 context 上调用 getSystemService(),而此时该上下文为 null

此外,Service 没有使用 LayoutInflater,因此您应该删除第 21 行以及引用它的所有内容,例如所有 EditText 小部件你不能使用的。或者,将此 Service 转换为 Activity,因为 Activity 有 UI,而 Service 没有。

You are trying to create a LayoutInflater in a static initializer. This will not work. Tactically, it will not work because you are trying to call getSystemService() on context, which is null at this point in time.

Moreover, a Service has no use for a LayoutInflater, so you should delete line 21 and everything that references it, such as all the EditText widgets that you cannot use. Or, convert this Service into an Activity, as an Activity has a UI, while a Service does not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文