问题描述
我刚升级到cordova 4.0 for android。我使用以下帖子在片段中加载cordova webview。
I just upgraded to cordova 4.0 for android. I used the following post to load a cordova webview inside a fragment..
https://github.com/Adobe-Marketing-Cloud-Apps/app-sample-android-phonegap/wiki/Embed-
https://github.com/Adobe-Marketing-Cloud-Apps/app-sample-android-phonegap/wiki/Embed-Webview-in-Android-Fragment
此代码在从3升级到cordova 4.0后不再有效。*
This code no longer works after upgrading to cordova 4.0 from 3.*
具体来说,第二行抛出异常...
Specifically, an exception is throw on this 2nd line...
LayoutInflater localInflater = inflater.cloneInContext(new CordovaContext(getActivity(), this));
View v = localInflater.inflate(R.layout.dialog_webview, container, false);
这个标签在我的布局文件中...
Where this tag is in my layout file...
<org.apache.cordova.CordovaWebView
android:layout_below="@+id/DialogTopBar"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id = "@+id/myWebView"
/>
异常消息...
$ b $
Does anyone have any ideas on how to get around this?
它看起来像cordova 4.0,CordovaWebView类已从..
It does look like since cordova 4.0, the CordovaWebView class was changed from..
public class CordovaWebView extends WebView
到
public interface CordovaWebView
推荐答案
不知道这是否正确,但我得到它的工作通过复制一些代码从新的4.0 CordovaActivity.java文件到我的片段以手动设置CordovaWebView。
Not sure if this is correct, but I got it working by copying some of the code from the new 4.0 CordovaActivity.java file into my fragment to setup the CordovaWebView manually.
步骤1.在布局中删除CordovaWebView xml标签。
Step 1. Remove CordovaWebView xml tag in layout.
添加以下代码片段以手动创建CordovaWebView并将其注入片段。
Step 2. Add in following code to fragment to manually create CordovaWebView and inject it into fragment.
private CordovaWebView webView;
// Read from config.xml:
protected CordovaPreferences preferences;
protected String launchUrl;
protected ArrayList<PluginEntry> pluginEntries;
protected CordovaInterfaceImpl cordovaInterface;
protected void loadConfig() {
ConfigXmlParser parser = new ConfigXmlParser();
parser.parse(getActivity());
preferences = parser.getPreferences();
preferences.setPreferencesBundle(getActivity().getIntent().getExtras());
preferences.copyIntoIntentExtras(getActivity());
launchUrl = parser.getLaunchUrl();
pluginEntries = parser.getPluginEntries();
// Config.parser = parser;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LayoutInflater localInflater = inflater.cloneInContext(new CordovaContext(getActivity(), this));
View v = localInflater.inflate(R.layout.dialog_webview, container, false);
cordovaInterface = new CordovaInterfaceImpl(getActivity());
if(savedInstanceState != null)
cordovaInterface.restoreInstanceState(savedInstanceState);
loadConfig();
webView = new CordovaWebViewImpl(CordovaWebViewImpl.createEngine(getActivity(), preferences));
webView.getView().setId(100);
RelativeLayout.LayoutParams wvlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
wvlp.addRule(RelativeLayout.BELOW,R.id.DialogTopBar);
webView.getView().setLayoutParams(wvlp);
if (!webView.isInitialized()) {
webView.init(cordovaInterface, pluginEntries, preferences);
}
cordovaInterface.onCordovaInit(webView.getPluginManager());
// webView = (SystemWebView)v.findViewById(R.id.myWebView);
// Config.init(getActivity());
((RelativeLayout)v).addView(webView.getView());
}
这篇关于Cordova webview在Android Fragment在Cordova 4.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!