【问题标题】:Starting Android IntentService with explicit intent NullPointerException使用显式意图 NullPointerException 启动 Android IntentService
【发布时间】:2015-01-27 18:12:42
【问题描述】:

我正在尝试从我的主要活动中的点击处理程序启动IntentService。我现在正在学习Intents,但还不太明白。我不确定我应该如何在这里实例化我的意图并将其传递给startService。我不知道为什么我必须做这样的事情,Intent 不会在我的服务中使用,我知道。我不知道如何调试这个问题。

来自点击回调:

    this.commute = new Commute();

    locationService = new LocationService();
    locationService.setCommute(commute);
    // com.orm.SugarApp@8b2e18f is this.getApplicationContext() ...
    Context context = this.getBaseContext();
    System.out.println("!!!!!!!!!!!!!!!!!!!!!");
    System.out.println(context);
    Intent intent = new Intent(context, LocationService.class);
    System.out.println(locationService);
    System.out.println(intent);
    locationService.startService(intent);  // <---- OFFENDING LINE
    System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@");

    chronometer.setBase(SystemClock.elapsedRealtime());
    chronometer.start();
    commute.start();

traceback之前的logcat,没有一个对象是null...:

01-27 09:53:44.465    2718-2718/org.skyl.commutetracker I/System.out﹕ !!!!!!!!!!!!!!!!!!!!!
01-27 09:53:44.466    2718-2718/org.skyl.commutetracker I/System.out﹕ android.app.ContextImpl@8b2e18f
01-27 09:53:44.466    2718-2718/org.skyl.commutetracker I/System.out﹕ org.skyl.commutetracker.services.LocationService@387dcc1c
01-27 09:53:44.466    2718-2718/org.skyl.commutetracker I/System.out﹕ Intent { cmp=org.skyl.commutetracker/.services.LocationService }
01-27 09:53:44.466    2718-2718/org.skyl.commutetracker D/AndroidRuntime﹕ Shutting down VM
01-27 09:53:44.481    2718-2718/org.skyl.commutetracker E/AndroidRuntime﹕ FATAL EXCEPTION: main

这会导致以下回溯:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.ComponentName android.content.Context.startService(android.content.Intent)' on a null object reference
            at android.content.ContextWrapper.startService(ContextWrapper.java:515)
            at org.skyl.commutetracker.MainActivity.startCommute(MainActivity.java:86)
            at org.skyl.commutetracker.MainActivity.toggleClick(MainActivity.java:67)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)

【问题讨论】:

    标签: java android android-intent android-service


    【解决方案1】:

    您不想在第二行实例化 LocationService。

    你会想要使用以下之一:

    context.bindService(intent, serviceConnection, boolean);
    

    context.startService(intent);
    

    对于您的 IntentService,您可能希望使用 startService。绑定服务与意图服务不同。

    此外,如果您想将通勤传递给 Intent 服务,您可能希望将其作为额外的传递给 Intent。

    intent.putExtra(String key, Parcelable item)
    

    putExtra 是重载的,所以只要 Commute 对象实现了所需的接口之一,它应该可以正常工作。有关 Intent 对象的更多信息,请访问http://developer.android.com/reference/android/content/Intent.html

    你可以在这里找到更多关于服务的信息http://developer.android.com/guide/components/services.html

    【讨论】:

    • 那么,我如何将Commute 对象传递给LocationService
    • 在 IntentService 中,您可以通过调用 getExtra 方法之一来恢复通勤。有关意图的链接应该有可用的 API。
    【解决方案2】:

    您无需创建服务对象来启动它,您只需使用context 来启动它

    Intent intent = new Intent(context, LocationService.class);
    context.startService(intent);
    

    【讨论】:

    • 那么,我如何将Commute 对象传递给LocationService
    猜你喜欢
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多