Unable to start service [service name] with null
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开发者_StackOverflow社区 help
According to the documentation for onStart()
(actually onStartCommand()
but the parameters are the same):
intent The Intent supplied to startService(Intent), as given. This may be null if the service is being restarted after its process has gone away, and it had previously returned anything except START_STICKY_COMPATIBILITY.
Hi just to add on to this, so the workaround is just to add a if(intent!=null) before the getAction()? If I do this, will the service start up properly later on it's own? Meaning the OS actually helps me start the service properly in the case of a null intent. I'm afraid of ending up in a situation where the service doesn't get started at all.
精彩评论