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

如何解决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

解决方法

问题似乎出在:

com.mwr.example.sieve.AuthService$MessageHandler.handleMessage()

如果这个方法是你的“handleMessage(Message param1Message) {..}”,那么当你这样做时

bundle.getString("com.mwr.example.sieve.PIN");

“捆绑”为 NULL。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?