Cordova webview inside Android Cordova 4.0 上的片段

Cordova webview inside Android Fragment on Cordova 4.0

我刚刚升级到 android 的 cordova 4.0。我使用以下 post 在片段中加载 cordova webview..

https://github.com/Adobe-Marketing-Cloud-Apps/app-sample-android-phonegap/wiki/Embed-Webview-in-Android-Fragment

从 3.*

升级到 cordova 4.0 后,此代码不再有效

具体来说,第 2 行抛出异常...

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: Binary XML file line #43: Class is not a View org.apache.cordova.CordovaWebView

有没有人知道如何解决这个问题?

看起来确实从 cordova 4.0 开始,CordovaWebView class 已从..

更改
public class CordovaWebView extends WebView

public interface CordovaWebView

不确定这是否正确,但我通过将新 4.0 CordovaActivity.java 文件中的一些代码复制到我的片段中以手动设置 CordovaWebView 来使其工作。

第 1 步。删除布局中的 CordovaWebView xml 标记。

第二步,在fragment中添加如下代码,手动创建CordovaWebView并注入到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());
}

我在这里找到了一种方法: http://www.catharinegeek.com/embed-cordova-webview-in-android-native-app/

诀窍是在布局中使用 SystemWebView xml

 <org.apache.cordova.engine.SystemWebView
        android:id="@+id/cordovaView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

我遇到了同样的问题,我已经解决了,如下所示。在第一个示例中,我将 CordovaWebView 嵌入到 activity 中。注意 cordova 4.0,我们不能在布局中包含 CordovaWebView,因此在以前的 cordova 版本中,CordovaWebView 扩展了 WebView,但由于 Cordova 4 这是一个接口,因此我们必须包含 org.apache.cordova.engine.SystemWebView。如果您查看代码,您会发现必须覆盖通过 super.init().

调用的方法 makeWebView() 和 createViews()

示例 1:Activity

中的 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>