gpt4 book ai didi

Javascript 界面与 android 不工作

转载 作者:行者123 更新时间:2023-12-01 04:07:51 25 4
gpt4 key购买 nike

我正在尝试将变量从 Android 传递到 JavaScript。单击 map 按钮时,它将使用以下 onCreate 方法打开此类:

@SuppressLint("JavascriptInterface")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maps);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

myWebView = (WebView) findViewById(R.id.webview);
setMyWebviewSettings(myWebView.getSettings());

myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebChromeClient(new WebChromeClient());

Bundle extras = getIntent().getExtras();
double[] coordinates = setCoordinates(extras);

myWebView.addJavascriptInterface(new JavaScriptInterface(coordinates), "JSInterface");

myWebView.loadUrl("http://10.0.0.32:8887/services/maps/examples/basic-example.htm");
}

这是setMyWebviewSettings方法:

private void setMyWebviewSettings(WebSettings MyWebviewSettings){
MyWebviewSettings.setAllowFileAccessFromFileURLs(true);
MyWebviewSettings.setAllowUniversalAccessFromFileURLs(true);
MyWebviewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
MyWebviewSettings.setJavaScriptEnabled(true);
MyWebviewSettings.setDomStorageEnabled(true);
MyWebviewSettings.setJavaScriptCanOpenWindowsAutomatically(true);
MyWebviewSettings.setBuiltInZoomControls(true);
MyWebviewSettings.setAllowFileAccess(true);
MyWebviewSettings.setSupportZoom(true);
}

然后这是JavaScriptInterface:

public class JavaScriptInterface {

double lat;
double lon;
double pan;

JavaScriptInterface(double[] coordinate) {
this.lat = coordinate[0];
this.lon = coordinate[1];
this.pan = coordinate[2];
}

public double getLatValue() {
return lat;
}

public double getLonValue() {
return lon;
}

public double getPanValue() {
return pan;
}
}

这是setCooperatives方法:

private double[] setCoordinates(Bundle extras) {

double[] coordinates = new double[3];

coordinates[0] = 45.9049;
coordinates[1] = 13.3177;
coordinates[2] = 15;

if (extras != null) {
coordinates[0] = extras.getDouble("lat");
coordinates[1] = extras.getDouble("lon");
coordinates[2] = extras.getDouble("pan");
}
return coordinates;
}

一切正常,只是不加载 map 。 Bundle extrasNull。如何解决这个问题?

最佳答案

确保在创建启动第二个 Activity 的 Intent 时传递值。如果 bundle 为空,则意味着它们不包含在 Intent 中。

关于Javascript 界面与 android 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41679948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com