hi_boygirl
liwg_
采纳率25%
2016-09-05 02:54 阅读 898
已采纳

有个关于handler的问题,求帮助

下面的代码,为什么我一调用stop()方法就会报空指针的异常?
public class SingleUtil {
private Context con;
private HandlerThread handlerThread;
private Handler handler;
private static final int THREAD_NUMBER_ONE = 1;
int num = 0;

public SingleUtil() {
}

public SingleUtil(Context con) {
    this.con = con;
}

public void SingleLoop(final String a) {
    handlerThread = new HandlerThread("name");
    handlerThread.start();
    handler = new Handler(handlerThread.getLooper()) {
        @Override
        public void handleMessage(Message msg) {
            num = num++;
            String str = null;
            try {
                //访问网络耗时操作
                str = PhoneUtil.information(a);
                UpLoading ul = new UpLoading(con);
                ul.sign(str);
            } catch (Exception e) {
                e.printStackTrace();
            }
            handler.sendEmptyMessageDelayed(THREAD_NUMBER_ONE, 5*1000);//消息循环
        }

    };
    handler.sendEmptyMessage(THREAD_NUMBER_ONE);//启动消息
}

public void stop(){
    Message msg = handler.obtainMessage();
    handler.removeMessages(THREAD_NUMBER_ONE);
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    qq_31288799 沙鸥丶 2016-09-05 06:16

    是在还没初始化之前调了stop吗?

    点赞 评论 复制链接分享

相关推荐