本文介绍了无法使用 null 启动服务 [服务名称]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近编写了一个 Android Widget 并在 Emulator 和我的 Galaxy S 上对其进行了测试,它在两者上都运行良好,在我将相同的内容发布到 android 市场之后,现在我收到了一些错误报告.

i recently coded a Android Widget and tested it on Emulator as well as my Galaxy S , it worked fine on both, after i posted the same to android market now i am getting some error reports.

我在 Widget 类的 onUpdate 中声明一个服务,如下所示:

i am stating a service in the onUpdate of Widget Class like this:

if (appWidgetIds != null) {
    final int N = appWidgetIds.length;

    // Perform this loop procedure for each App Widget that belongs to
    // this
    // provider
    for (int i = 0; i < N; i++) {
        // Start Service for each instance
        int appWidgetId = appWidgetIds[i];
        Intent active = new Intent(context, DialerService.class);
        active.setAction("Start");
        active.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                appWidgetId);

        context.startService(active);
    }
}

有些人得到的错误是:

java.lang.RuntimeException: Unable to start service dialer.impact.DialerService@45f926f0 with null: java.lang.NullPointerException
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3282)
at android.app.ActivityThread.access$3600(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2211)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:144)
at android.app.ActivityThread.main(ActivityThread.java:4937)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
**at dialer.impact.DialerService.onStart(DialerService.java:18)**
at android.app.Service.onStartCommand(Service.java:420)
at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3267)
... 10 more

error 在 ServiceClass 的第 18 行指出了一个 NullPointerException,它是这样的:

error states a NullPointerException on line 18 of the ServiceClass which is this:

@Override
public void onStart(Intent intent, int startId) {
    //Line 18th
            String command = intent.getAction();
    int appWidgetId = intent.getExtras().getInt(
            AppWidgetManager.EXTRA_APPWIDGET_ID);
    RemoteViews remoteView = new RemoteViews(getApplicationContext()
            .getPackageName(), R.layout.main);
    AppWidgetManager appWidgetManager = AppWidgetManager
            .getInstance(getApplicationContext());
}

第 18 行是 String command = intent.getAction();

intent 为空的原因可能是什么请帮忙

what could be the reason for intent being nullplease help

推荐答案

根据 onStart()(其实是onStartCommand() 但是参数是一样的):

According to the documentation for onStart() (actually onStartCommand() but the parameters are the same):

intent 提供给 startService(Intent) 的 Intent,如给定.这可能为空如果服务正在进程结束后重新启动离开,它之前已经返回除了START_STICKY_COMPATIBILITY.

这篇关于无法使用 null 启动服务 [服务名称]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 22:13