在服务中发送广播,有个空指针异常

黑白_hb 2016-05-30 09:46:44
这是代码:

package com.tomwin.service;

import java.util.Random;

import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;


public class sendBroadcast extends Service{
sendBroadcast broadcast=new sendBroadcast();

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.i("service", "服务启动");
startThread();
}

public void startThread(){
Thread thread=new Thread(new Runnable() {

@Override
public void run() {

while (true) {
byte[] data=new byte[14];
for (int i = 0; i < data.length; i++) {
Random r=new Random();
data[i]=(byte) r.nextInt(256);
}
try {
Log.i("service", "======="+data.length);

Intent intent=new Intent("android.intent.mcu.CANBOX");
Bundle bundle=new Bundle();
bundle.putByteArray("values", data);
intent.putExtras(bundle);
sendBroadcast(intent);
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
});
thread.start();

}

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

return null;
}

}


好吧 。出现了新的问题 。现在这个服务起不来了。
看下 :
结果这里没问题的


那么问题在哪呢
...全文
170 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
insomnia的祝福 2016-05-30
  • 打赏
  • 举报
回复
服务没起来,先检查你Manifest文件里有没有定义sendBroadcast 这个Service
黑白_hb 2016-05-30
  • 打赏
  • 举报
回复
奇怪 还有哪里没写到位呢

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧