我不知道为什么以下代码在模拟器中工作正常并且在Android手机中失败。
package com.mycompany.myapp;
import com.codename1.components.WebBrowser;
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Label;
import com.codename1.ui.plaf.UIManager;
import com.codename1.ui.util.Resources;
import com.codename1.io.Log;
import com.codename1.javascript.JSFunction;
import com.codename1.javascript.JSObject;
import com.codename1.javascript.JavascriptContext;
import com.codename1.ui.BrowserComponent;
import com.codename1.ui.Toolbar;
import com.codename1.ui.layouts.BorderLayout;
import java.io.IOException;
public class MyApplication {
private Form current;
private Resources theme;
public void init(Object context) {
theme = UIManager.initFirstTheme("/theme");
// Enable Toolbar on all Forms by default
Toolbar.setGlobalToolbar(true);
// Pro only feature, uncomment if you have a pro subscription
// Log.bindCrashProtection(true);
}
public void start() {
if(current != null){
current.show();
return;
}
Form hi = new Form("Hi World");
final WebBrowser b = new WebBrowser(){
@Override
public void onLoad(String url) {
// Placed on onLoad because we need to wait for page
// to load to interact with it.
BrowserComponent c = (BrowserComponent)this.getInternal();
// Create a Javascript context for this BrowserComponent
JavascriptContext ctx = new JavascriptContext(c);
// Now use the context to interact with Javascript environment....
JSObject window = (JSObject)ctx.get("window");
window.set("addAsync", new JSFunction(){
public void apply(JSObject self, final Object[] args) {
Double a = (Double)args[0];
Double b = (Double)args[1];
JSObject callback = (JSObject)args[2];
double result = a.doubleValue() + b.doubleValue();
Dialog.show("testing",result +"", "ok","");
callback.call(new Object[]{new Double(result)});
}
});
// Dialog.show("Content", pageContent, "OK", "Cancel");
}
};
// b.setURL("jar:///mydata.html");
b.setPage("<html><body>"
+ ""+
" <head>\n" +
" <title>Addition Example</title>\n" +
" </head>\n" +
" <body>\n" +
" <div>Addition Example</div>\n" +
" <p><input type=\"text\" size=\"4\" id=\"input1\"/> +\n" +
" <input type=\"text\" size=\"4\" id=\"input2\"/> =\n" +
" <span id=\"result\"></span>\n" +
" </p>\n" +
" <p><button id=\"calculate\">Calculate</button></p>\n" +
" <script>\n" +
" \n" +
" document\n" +
" .getElementById('calculate')\n" +
" .addEventListener('click', function(){\n" +
" var aField = document.getElementById('input1');\n" +
" var bField = document.getElementById('input2');\n" +
" var a = parseFloat(aField.value);\n" +
" var b = parseFloat(bField.value);\n" +
" window.addAsync(a, b, function(result){\n" +
" document.getElementById('result').innerHTML = result;\n" +
" });\n" +
" }, true);\n" +
" \n" +
" \n" +
" </script>\n"
+ ""
+ " </body></html>", null);
hi.setLayout(new BorderLayout());
hi.addComponent(BorderLayout.CENTER, b);
hi.show();
}
public void stop() {
current = Display.getInstance().getCurrent();
if(current instanceof Dialog) {
((Dialog)current).dispose();
current = Display.getInstance().getCurrent();
}
}
public void destroy() {
}
}
我在我的模拟器上使用了代码来测试它的功能,它运行正常。当我在我的Android手机上构建并安装它时,令我惊讶的是,它没有按预期工作。我不知道我需要添加什么来让我在手机上工作。请帮助我。感谢
模拟器上的屏幕短视
当我点击手机上的计算按钮时,它没有收到任何消息,也没有进行任何计算。请问我只是用它来测试运行。我对这个项目走得很远。谢谢你的帮助
答案 0 :(得分:0)
这是Javascript网桥中的一个错误,它影响了某些版本的Android(并非所有版本)。我修复了它here,以便您的代码在星期五的下一次服务器更新后工作。
如果您将“setPage()”的第二个参数更改为某个虚拟网址(例如http://www.codenameone.com),那么它也应该同时解决此问题。