我有两个布局(start.xml
和 activity_main.xml
),我的第一个 Activity GubStartActivity.java
使用“start.xml”使用调用 WebView
public void disparaweb()
{
setContentView(R.layout.activity_main);
myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setAllowContentAccess(true);
myWebView.loadData("", "text/html", null);
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl(Global.urlstr);
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
}
我使用 WebAppInterface.java 作为我的 .net 应用程序的 JavaScript 接口(interface)。
一切正常,但我想更改 WebAppInterface 中的 URL,但无法使用 findViewById 获取 WebView
,它总是返回 null,无论我使用什么。
如何获取 WebAppInterface.java
中的 myWebView 元素?
最佳答案
好吧,我想如果你将 webview 作为参数传递给你的 WebAppInterface 它应该可以工作,在你的 WebAppInterface 中更改它
WebAppInterface(Context c) {
mContext = c;
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBlueProbeAdapter = BluetoothAdapter.getDefaultAdapter();
}
至
WebAppInterface(Context c, WebView webView) {
mContext = c;
this.myWebView=webView;
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBlueProbeAdapter = BluetoothAdapter.getDefaultAdapter();
}
当你设置这个时
myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
将其设置为
myWebView.addJavascriptInterface(new WebAppInterface(this, myWebView), "Android");
关于java - findViewById 为另一个类上的 WebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29264852/