Android:绑定到远程服务:服务崩溃

问题描述

我正在尝试绑定到 Drozer test app (sieve) 的远程服务,但我不断使用 NullPointerException 使该应用程序崩溃,我不明白为什么。

基本思想是向sieve应用发送PIN码,它会返回一个密码。

测试应用的代码包括以下几行:

public void handleMessage(Message param1Message) {
switch (param1Message.what) {...
case 2354: 
 if (str.arg1 == 9234) { 
  String str1 = bundle.getString("com.mwr.example.sieve.PIN");
  if (AuthService.this.verifyPin(str1)){ 
   Bundle bundle1 = new Bundle();
   bundle1.putString("com.mwr.example.sieve.PASSWORD",AuthService.this.getKey()); 
   ...

我正在尝试使用我自己的应用程序(使用 Kotlin)跳进去:

val serviceIntent = Intent()
serviceIntent.setClassName("com.mwr.example.sieve","com.mwr.example.sieve.AuthService")
bindService(serviceIntent,myConnection,Context.BIND_AUTO_CREATE)

        if (!isBound) {
            Toast.makeText(this,"Please press again to bind",Toast.LENGTH_LONG).show()
            return
        } else {
            Toast.makeText(this,"Service bound",Toast.LENGTH_LONG).show()
        }

        try {
            val msg = Message.obtain()
            val bundle = Bundle()
            bundle.putString("com.mwr.example.sieve.PIN","1234")
            msg.what = 2354
            msg.arg1 = 9234
            msg.data = bundle
            myService?.send(msg)

        } catch (e: Exception) {
            e.printStackTrace()
        }

筛选应用程序崩溃并显示以下消息:

com.mwr.example.sieve E/AndroidRuntime: FATAL EXCEPTION: m_AuthService
    Process: com.mwr.example.sieve:remote,PID: 5752
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.BaseBundle.getString(java.lang.String)' on a null object reference
        at com.mwr.example.sieve.AuthService$MessageHandler.handleMessage(AuthService.java:156)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.os.HandlerThread.run(HandlerThread.java:61)

在我看来,它获得了一个空值而不是预期值,但我不明白它还在期待什么。通过使用 Drozer 工具本身,我可以访问我感兴趣的数据:

dz> run app.service.send com.mwr.example.sieve com.mwr.example.sieve.AuthService --msg 2354 9234 1 --extra string com.mwr.example.sieve.PIN 1234 --bundle-as-obj

        Got a reply from com.mwr.example.sieve/com.mwr.example.sieve.AuthService:
          what: 5
          arg1: 41
          arg2: 0
          Extras
            com.mwr.example.sieve.PASSWORD (String) : H4ck3d

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)