gpt4 book ai didi

android - 无法启动服务 [服务名称] 为 null

转载 作者:可可西里 更新时间:2023-11-01 18:52:13 26 4
gpt4 key购买 nike

我最近编写了一个 Android 小部件,并在 Emulator 和我的 Galaxy S 上对其进行了测试,它在两者上都运行良好,在我将其发布到 android market 之后,现在我收到了一些错误报告。

我在 Widget 类的 onUpdate 中这样声明一个服务:

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

错误指出 ServiceClass 第 18 行的 NullPointerException 是这样的:

@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 为 null 的原因可能是什么请帮忙

最佳答案

根据 onStart() 的文档(实际上是onStartCommand() 但参数是一样的):

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.

关于android - 无法启动服务 [服务名称] 为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4679654/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com