【问题标题】:Android Service Unable to start intent & NullPointerExceptionAndroid 服务无法启动意图和 NullPointerException
【发布时间】:2012-08-20 21:44:12
【问题描述】:

我正在创建一个后台服务(在它自己的进程中)并且在让它工作时遇到了很多麻烦。我正在尝试在应用程序启动时启动它,并且在日志中出现无法启动服务的意图错误。我一直在浏览论坛、示例(和谷歌),但找不到我做错了什么。

这是我得到的错误:
E/AndroidRuntime(1398): java.lang.RuntimeException: Unable to start service com.test.alarms.AlarmService@41550cb0 with Intent { cmp=xxxx }: java.lang.NullPointerException

在我的活动中:

startService(new Intent(AlarmService.class.getName()));

服务类是:

package com.test.alarms;


public class AlarmService extends Service{

Context context; 

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
//code to execute when the service is first created
}

@Override
public void onDestroy() {
//code to execute when the service is shutting down
}

@Override
public void onStart(Intent intent, int startid) {
//code to execute when the service is starting up
    Intent i = new Intent(context, StartActivity.class);
    PendingIntent detailsIntent = PendingIntent.getActivity(this, 0, i, 0);

    NotificationManager notificationSingleLine = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification notificationDropText = new Notification(R.drawable.ic_launcher, "Alarm for...", System.currentTimeMillis());

    CharSequence from = "Time for...";
    CharSequence message = "Alarm Text";        
    notificationDropText.setLatestEventInfo(this, from, message, detailsIntent);

    notificationDropText.vibrate = new long[] { 100, 250, 100, 500};        
    notificationSingleLine.notify(0, notificationDropText);
}

}

清单文件有:

<service
        android:name=".AlarmService"
        android:process=":remote">
        <intent-filter>
            <action android:name="com.test.alarms.AlarmService"/>
        </intent-filter>
    </service>

谢谢,

【问题讨论】:

    标签: android service


    【解决方案1】:

    也许问题是您已经覆盖了 OnCreate 和 OnDestroy,但您没有调用 super.Oncreate() 和 super.onDestroy()。

    如果这不起作用,请尝试

    startService(new Intent(context , AlarmService.class));
    

    编辑: 在 onStart 中也使用这个

    Intent i = new Intent(this, StartActivity.class);
    

    而不是

    Intent i = new Intent(context, StartActivity.class);
    

    【讨论】:

    • E/AndroidRuntime(1398): java.lang.RuntimeException: Unable to start service com.test.alarms.AlarmService@41550cb0 with Intent { cmp=xxxx }: java.lang.NullPointerException
    • 您的问题标题具有误导性,您应该说您遇到空指针异常。您是否尝试过使用 this 而不是编辑中建议的上下文
    • 您应该发布整个调用堆栈并发布在您收到 Nullpointer 异常的哪一行。编辑您的问题
    • 感谢大家的帮助,挖掘后发现需要添加震动权限
    【解决方案2】:

    根据documentation,您需要在onStartCommand(Intent intent,int,int); 内部检查传递意图是否为null(用于在进程终止后重新启动)或不(正常操作);

    更好的设计,

    public int onStartCommand(Intent intent, int flags, int startId) {
        if(intent != null){
            handleCommand(intent);
        }
        // We want this service to continue running until it is explicitly
        // stopped, so return sticky.
        return START_STICKY;
    }
    

    阅读here更多

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-27
      • 1970-01-01
      • 2018-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多