如何解决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 举报,一经查实,本站将立刻删除。