当我关闭我的应用程序时,我收到一个对话框,说“不幸的是,MyApp已经停止了”。和一个确定按钮。关闭,我的意思是从第一个屏幕按下后退按钮。该应用程序确实关闭,但几秒钟后会出现此对话框。
我正在使用PhoneGap 3.2.0。在更新到3.x.x之前没有发生过。我不确定在此之后究竟是哪个版本的罪魁祸首。
Eclipse中的LogCat会在发生这种情况时发出:
01-13 15:20:42.764: D/CordovaActivity(32367): onMessage(exit,null)
01-13 15:20:42.829: D/CordovaActivity(32367): Paused the application!
01-13 15:20:42.839: W/PluginManager(32367): THREAD WARNING: exec() call to App.exitApp blocked the main thread for 74ms. Plugin should use CordovaInterface.getThreadPool().
01-13 15:20:43.219: W/IInputConnectionWrapper(32367): showStatusIcon on inactive InputConnection
01-13 15:20:43.254: D/CordovaActivity(32367): CordovaActivity.onDestroy()
01-13 15:20:43.259: D/CordovaWebView(32367): >>> loadUrlNow()
01-13 15:20:43.284: D/AndroidRuntime(32367): Shutting down VM
01-13 15:20:43.284: W/dalvikvm(32367): threadid=1: thread exiting with uncaught exception (group=0x417752a0)
01-13 09:56:40.404: E/AndroidRuntime(13858): java.lang.RuntimeException: Unable to destroy activity {MyAppName}: java.lang.IllegalArgumentException: Receiver not registered: null
01-13 09:56:40.404: E/AndroidRuntime(13858): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3314)
01-13 09:56:40.404: E/AndroidRuntime(13858): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3332)
01-13 09:56:40.404: E/AndroidRuntime(13858): at android.app.ActivityThread.access$1200(ActivityThread.java:140)
01-13 09:56:40.404: E/AndroidRuntime(13858): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1280)
01-13 09:56:40.404: E/AndroidRuntime(13858): at android.os.Handler.dispatchMessage(Handler.java:99)
01-13 09:56:40.404: E/AndroidRuntime(13858): at android.os.Looper.loop(Looper.java:137)
01-13 09:56:40.404: E/AndroidRuntime(13858): at android.app.ActivityThread.main(ActivityThread.java:4898)
01-13 09:56:40.404: E/AndroidRuntime(13858): at java.lang.reflect.Method.invokeNative(Native Method)
01-13 09:56:40.404: E/AndroidRuntime(13858): at java.lang.reflect.Method.invoke(Method.java:511)
01-13 09:56:40.404: E/AndroidRuntime(13858): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
01-13 09:56:40.404: E/AndroidRuntime(13858): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
01-13 09:56:40.404: E/AndroidRuntime(13858): at dalvik.system.NativeStart.main(Native Method)
01-13 09:56:40.404: E/AndroidRuntime(13858): Caused by: java.lang.IllegalArgumentException: Receiver not registered: null
01-13 09:56:40.404: E/AndroidRuntime(13858): at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:654)
01-13 09:56:40.404: E/AndroidRuntime(13858): at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1277)
01-13 09:56:40.404: E/AndroidRuntime(13858): at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:385)
01-13 09:56:40.404: E/AndroidRuntime(13858): at org.apache.cordova.device.Device.onDestroy(Device.java:98)
01-13 09:56:40.404: E/AndroidRuntime(13858): at org.apache.cordova.PluginManager.onDestroy(PluginManager.java:332)
01-13 09:56:40.404: E/AndroidRuntime(13858): at org.apache.cordova.CordovaWebView.handleDestroy(CordovaWebView.java:833)
01-13 09:56:40.404: E/AndroidRuntime(13858): at org.apache.cordova.CordovaActivity.onDestroy(CordovaActivity.java:774)
01-13 09:56:40.404: E/AndroidRuntime(13858): at android.app.Activity.performDestroy(Activity.java:5370)
01-13 09:56:40.404: E/AndroidRuntime(13858): at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1113)
01-13 09:56:40.404: E/AndroidRuntime(13858): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3301)
01-13 09:56:40.404: E/AndroidRuntime(13858): ... 11 more
有什么想法吗?
到现在为止,我一直在使用这条线 navigator.app.exitApp(); 如果我在按下后退按钮时检测到我在index.html页面上,则关闭应用程序。
我尝试删除后退按钮侦听器,并在按下后退按钮时恢复默认行为。它仍然会导致同样的问题。
以下是一些缩短的代码段:
$(document).ready(function ()
{
document.addEventListener("deviceready", onDeviceReady, false);
}
function onDeviceReady()
{
document.addEventListener("backbutton", backButton, false);
});
function backButton()
{
var currentLocation = window.location.href.substr(window.location.href.lastIndexOf("/")+1);
switch (currentLocation) {
case 'index.html':
navigator.app.exitApp();
break;
}
更新2014/01/13 16:00: 不确定它是否有任何帮助,但我注意到如果我从index.html注释掉脚本src =“cordova.js”,它不会出现任何错误。
答案 0 :(得分:3)
在文件Device.java中使用此
替换OnDestroy
的内容
if( this.telephonyReceiver != null )
this.cordova.getActivity().unregisterReceiver(this.telephonyReceiver);
警告:这是隐藏原始问题,因为我从logcat获取的内容没有正确注册BroadcastReceiver。将权限添加到AndroidManifest.xml
suprnova之类的说法应该有效。
但是如果你不需要知道电话状态,也就是说,如果你正在接听电话,或类似的东西,你需要快速解决。
警告2 :当下一个手机版本出来时,可能会被覆盖。
答案 1 :(得分:2)
根据您的LogCat判断,问题在于Device.java
文件,并查看来源here,该类会注册BroadcastReceiver
以检查手机状态。
确保您已添加:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
到AndroidManifest.xml
。