service本地服务通信(无法查看状态)

可以绑定服务与解绑,但是无法查看状态
图片说明

也没有服务断开连接的提示

MainActivity的代码

public class MainActivity extends AppCompatActivity {
    MyService.MyBinder myBinder;
    //定义一个ServiceConnection对象
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            Log.v("MainActivity", "服务连接成功");
            myBinder = (MyService.MyBinder) iBinder;
        }
        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            Log.v("MainActivity", "服务断开连接");
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void onClick(View view) {
            switch (view.getId()) {
                case R.id.b1:
                    //绑定服务service
                    Intent intent = new Intent(this, MyService.class);
                    bindService(intent, connection, BIND_AUTO_CREATE);
                    break;
                case R.id.b2:
                    //解除绑定服务service
                    unbindService(connection);
                    break;
                case R.id.b3:
                    Toast.makeText(this, myBinder.getCount(), Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
        }
    }

以下是报错信息

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.sixteenapplication, PID: 2005
    java.lang.IllegalStateException: Could not execute method for android:onClick
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402)
    at android.view.View.performClick(View.java:5198)
    at android.view.View$PerformClick.run(View.java:21147)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
    at android.view.View.performClick(View.java:5198) 
    at android.view.View$PerformClick.run(View.java:21147) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
     Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x5
    at android.content.res.Resources.getText(Resources.java:312)
    at android.widget.Toast.makeText(Toast.java:286)
    at com.example.sixteenapplication.MainActivity.onClick(MainActivity.java:42)
    at java.lang.reflect.Method.invoke(Native Method) 
    at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397) 
    at android.view.View.performClick(View.java:5198) 
    at android.view.View$PerformClick.run(View.java:21147) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
I/Process: Sending signal. PID: 2005 SIG: 9
Application terminated.

1个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问

相似问题

0
启服务报错 config monitor windows service fail 怎么解决?
1
Linux网络通信出现No route to host
0
Win7 打开windows功能中 Indexing Service 失败
1
Nginx启动时service nginx start出现错误
0
reporting service 报表url访问
0
springboot的service实现类方法上添加@Transactional(rollbackFor = Exception.class)事物不回滚
3
阿里云 centos7 mysql服务无法停止,提示Failed to stop mysqld.service: Unit mysqld.service not loaded.
1
SOA服务架构中,service可以调用service吗可能会引发什么问题
1
在本地(windows)上可以查看png格式图片,在linux服务器上就看不到呢,会报异常java.lang.IllegalArgumentException: image == null!
3
@Service("userService")配置之后,在spring-security.xml文件<security:authentication-provider user-service-ref="userService">报错
1
添加利用Svchost启动的dll服务,无法启动怎么解决?
1
用荣耀9调试显示[ThreadDexHotfixMonitor]Bind hotfix monitor service fail!
2
救急,SpringMVC非Controller类中使用任务列队操作service无法连接数据库?
1
zuul从本地重定向到服务器的服务时找不到服务
1
打开虚拟机的时候,提示VMware Workstation 未能启动 VMware Authorization Service。您可以尝试手动启动 VMware Authorization Service。
1
Dubbo服务部署到服务器上,从本地访问服务器上的服务访问不通
3
本地程序通过MultipartFile形式上传图片,接收为NULL
0
网关(gateway)集成security认证的观念问题
0
在Activity和Service之间通信,用LocalBroadCast的方式,和用bindService的方式,有什么区别?
2
ssh框架,接口实现类调用service方法报空指针异常