14

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.

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

the error which some people are getting is:

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

Line 18 is the String command = intent.getAction();

what could be the reason for intent being null please help

4

2 回答 2

31

根据文档onStart()(实际上onStartCommand()但参数相同):

意图 提供给 startService(Intent) 的意图,如给定的。如果服务在其进程消失后重新启动,并且它之前返回了除 START_STICKY_COMPATIBILITY 之外的任何内容,则这可能为 null 。

于 2011-01-13T14:39:27.723 回答
4

嗨,只是为了补充一点,所以解决方法是在 getAction() 之前添加一个 if(intent!=null)?如果我这样做,该服务会在以后自行正常启动吗?这意味着操作系统实际上可以帮助我在无效意图的情况下正确启动服务。我害怕最终陷入服务根本无法启动的情况。

于 2011-05-26T04:02:05.053 回答