我刚刚升级到cordova 4.0 for android。我使用以下帖子在片段中加载cordova webview ..
https://github.com/Adobe-Marketing-Cloud-Apps/app-sample-android-phonegap/wiki/Embed-Webview-in-Android-Fragment
从3开始升级到cordova 4.0后,此代码不再有效。*
具体来说,这个第二行有例外......
LayoutInflater localInflater = inflater.cloneInContext(new CordovaContext(getActivity(), this));
View v = localInflater.inflate(R.layout.dialog_webview, container, false);
这个标签在我的布局文件中...
<org.apache.cordova.CordovaWebView
android:layout_below="@+id/DialogTopBar"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id = "@+id/myWebView"
/>
异常消息......
android.view.InflateException:二进制XML文件行#43:类不是View org.apache.cordova.CordovaWebView
有没有人对如何解决这个问题有任何想法?
看起来好像自Cordova 4.0以来,CordovaWebView类已经改变了......
public class CordovaWebView extends WebView
至
public interface CordovaWebView
不确定这是否正确,但我通过将新的4.0 CordovaActivity.java文件中的一些代码复制到我的片段来手动设置CordovaWebView。
步骤1.在布局中删除CordovaWebView xml标记。
步骤2.将以下代码添加到片段以手动创建CordovaWebView并将其注入片段。
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());
}
不确定这是否正确,但我通过将新的4.0 CordovaActivity.java文件中的一些代码复制到我的片段来手动设置CordovaWebView。
步骤1.在布局中删除CordovaWebView xml标记。
步骤2.将以下代码添加到片段以手动创建CordovaWebView并将其注入片段。
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());
}
我有同样的问题,我已经解决了它,如下图所示。在第一个示例中,我在活动中嵌入了CordovaWebView。小心使用cordova 4.0我们不能在我们的布局中包含CordovaWebView,因此在以前的cordova版本中CordovaWebView扩展了WebView,但由于Cordova 4这是一个接口,所以我们必须包含一个org.apache.cordova.engine.SystemWebView。如果你看到代码,你可以看到你必须覆盖通过super.init()调用的方法makeWebView()和createViews()。
示例1:活动中的Cordova
MainActivity.java
public class MainActivity extends CordovaActivity{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
super.init();
// Load your application
launchUrl = "file:///android_asset/www/index.html"
loadUrl(launchUrl);
}
@Override
protected CordovaWebView makeWebView() {
SystemWebView webView =SystemWebView)findViewById(R.id.cordovaWebView);
return new CordovaWebViewImpl(new SystemWebViewEngine(webView));
}
@Override
protected void createViews() {
appView.getView().requestFocusFromTouch();
}
}
activity_main.xml中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<org.apache.cordova.engine.SystemWebView
android:id="@+id/cordovaWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
我在这里找到了一种方法:
http://www.catharinegeek.com/embed-cordova-webview-in-android-native-app/
诀窍是在布局xml中使用SystemWebView
<org.apache.cordova.engine.SystemWebView
android:id="@+id/cordovaView"
android:layout_width="match_parent"
android:layout_height="match_parent" />