Phonegap - Android - 从本机调用javascript导致leakedwindow

时间:2011-11-25 15:59:24

标签: android cordova android-webview

我正在开发一个基于phonegap的应用程序,我还需要从Java本机代码调用javascript,反之亦然

所以我在droidGap活动(appView.addJavascriptInterface)中添加了一个JavascriptInterface。 但每次我用java调用javascript时,使用loadUrl(“javascript:alert('Hello')”),在几秒后出现错误:

/PhoneGapLog( 2839): file:///android_asset/phonegap.js: Line 930 : JSCallback Error:                   
 Request failed.
 I/Web Console( 2839): JSCallback Error: Request failed. at         
 file:///android_asset/phonegap.js:930

然后,再过几秒钟,应用程序崩溃,

E/WindowManager( 2839): Activity com.package.act has leaked                               
window com.android.internal.policy.impl.PhoneWindow$DecorView@2afbfb08 that was      
originally added here
E/WindowManager( 2839): android.view.WindowLeaked: Activity 
com.package.act has leaked window      
com.android.internal.policy.impl.PhoneWindow$DecorView@2afbfb08 that was originally added here
E/WindowManager( 2839):         at android.view.ViewRoot.<init>(ViewRoot.java:265)
E/WindowManager( 2839):         at   android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
E/WindowManager( 2839):         at          android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
E/WindowManager( 2839):         at android.view.Window$LocalWindowManager.addView(Window.java:424)
E/WindowManager( 2839):         at android.app.Dialog.show(Dialog.java:241)
E/WindowManager( 2839):         at android.app.AlertDialog$Builder.show(AlertDialog.java:816)
E/WindowManager( 2839):         at com.phonegap.DroidGap$6.run(DroidGap.java:1483)
E/WindowManager( 2839):         at android.os.Handler.handleCallback(Handler.java:587)  
E/WindowManager( 2839):         at android.os.Handler.dispatchMessage(Handler.java:92)
E/WindowManager( 2839):         at android.os.Looper.loop(Looper.java:123)
E/WindowManager( 2839):         at android.app.ActivityThread.main(ActivityThread.java:3701)
E/WindowManager( 2839):         at java.lang.reflect.Method.invokeNative(Native Method)
E/WindowManager( 2839):         at java.lang.reflect.Method.invoke(Method.java:507)
E/WindowManager( 2839):         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
E/WindowManager( 2839):         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624) 
E/WindowManager( 2839):         at dalvik.system.NativeStart.main(Native Method)

这是我的代码:

public class Bukkett extends DroidGap {

private JavascriptInterface itf;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.init();

    itf = new JavascriptInterface();
    appView.addJavascriptInterface(itf, "Interface");

    this.setIntegerProperty("loadUrlTimeoutValue", 70000);
    super.loadUrl("file:///android_asset/html/index.html");
}

public class JavascriptInterface {
    public void sayHello() {
        loadUrl("javascript:alert('hello');");      
         }
    }
}

在我的.js文件中,我只是致电

 if (window.Interface)
         window.Interface.sayHello();

警报信息显示良好,但在此之后应用程序崩溃。

2 个答案:

答案 0 :(得分:2)

如果你需要调用你用JavaScript编写的Java代码,你应该编写一个PhoneGap Android插件:

http://wiki.phonegap.com/w/page/36753494/How%20to%20Create%20a%20PhoneGap%20Plugin%20for%20Android

我们已经处理了JS与Java之间的通信中出现的许多问题,并且又回来了。在您的Android插件中,您可以随时调用sendJavascript()以直接调用您的JavaScript,但在大多数情况下,最好返回PluginResult。

答案 1 :(得分:0)

sendJavaScript优于loadURL,因为它不会导致布局刷新