老师为什么这里会抛出异常

老师为什么这里会抛出异常


2019-04-27 16:52:56.007 11383-11383/com.example.servicetest D/MyService: onCreate executed

2019-04-27 16:52:56.008 11383-11383/com.example.servicetest D/MyService: onStartCommand executed

2019-04-27 16:53:06.980 11383-11383/com.example.servicetest D/MyService: onBind executed

2019-04-27 16:53:06.982 11383-11383/com.example.servicetest E/AndroidRuntime: FATAL EXCEPTION: main

    Process: com.example.servicetest, PID: 11383

    java.lang.RuntimeException: Unable to bind to service com.example.servicetest.MyService@eccd01a with Intent { cmp=com.example.servicetest/.MyService }: java.lang.UnsupportedOperationException: Not yet implemented

        at android.app.ActivityThread.handleBindService(ActivityThread.java:3445)

        at android.app.ActivityThread.-wrap2(Unknown Source:0)

        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1688)

        at android.os.Handler.dispatchMessage(Handler.java:105)

        at android.os.Looper.loop(Looper.java:164)

        at android.app.ActivityThread.main(ActivityThread.java:6541)

        at java.lang.reflect.Method.invoke(Native Method)

        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)

        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

     Caused by: java.lang.UnsupportedOperationException: Not yet implemented

        at com.example.servicetest.MyService.onBind(MyService.java:36)

        at android.app.ActivityThread.handleBindService(ActivityThread.java:3431)

        at android.app.ActivityThread.-wrap2(Unknown Source:0) 

        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1688) 

        at android.os.Handler.dispatchMessage(Handler.java:105) 

        at android.os.Looper.loop(Looper.java:164) 

        at android.app.ActivityThread.main(ActivityThread.java:6541) 

        at java.lang.reflect.Method.invoke(Native Method) 

        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 

        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

代码在这里:

package com.example.servicetest;

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

public class MyService extends Service {
    public MyService() {
    }

    @Override
    public void onCreate() {
        Log.d("MyService","onCreate executed");
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("MyService","onStartCommand executed");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {

        super.onDestroy();
        Log.d("MyService","onDestroy executed");
    }


    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        Log.d("MyService","onBind executed");
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public boolean onUnbind(Intent intent) {
        Log.d("MyService","onunbind executed");
        return super.onUnbind(intent);
    }
}


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
irista23 2019-11-25 15:19:29

参考下这里视频,老师后来处理把异常删除掉了

http://img1.sycdn.imooc.com//climg/5ddb806a099b9fb312200881.jpg

好帮手慕雪 2019-04-28 10:50:24

这是你自己抛出的异常啊。你再好好看一下课,你这里要返回一下Binder对象。

http://img1.sycdn.imooc.com//climg/5cc51b7a00010ce405460118.jpg祝:学习愉快

  • 杜尔 #1
    视频里老师也有这句,但是老师没闪退
    2019-11-25 13:29:30
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师