【问题标题】:CordovaWebView Error: org.apache.cordova.CordovaWebView cannot be cast to android.view.ViewCordovaWebView 错误:org.apache.cordova.CordovaWebView 无法转换为 android.view.View
【发布时间】:2015-09-16 18:40:07
【问题描述】:

我在 Android 上实现 Cordova 5.1.1 时遇到了一些问题。 我正在尝试在我的应用程序中嵌入一个 CordovaWebView。 我已经按照他们官方site 中描述的所有说明进行操作,但是我不断收到错误消息“org.apache.cordova.CordovaWebView 无法转换为 android.view.View”。我已经搜索了很多帮助,但我找不到任何可以帮助我的东西。这是我的代码:

活动:

public class CordovaTestActivity extends Activity implements CordovaInterface {

protected CordovaPlugin activityResultCallback = null;
protected boolean activityResultKeepRunning;
protected boolean keepRunning = true;
private final ExecutorService threadPool = Executors.newCachedThreadPool();
CordovaWebView cwv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cordova_test);
    cwv = (CordovaWebView) findViewById(R.id.tutorialView);
    Config.init(this);
    cwv.loadUrl(Config.getStartUrl());
}

@Override
public Activity getActivity() {
    return this;
}

@Override
public ExecutorService getThreadPool() {
    return threadPool;
}

@Override
public Object onMessage(String arg0, Object arg1) {
    return null;
}

@Override
public void setActivityResultCallback(CordovaPlugin plugin) {
    this.activityResultCallback = plugin;
}

@Override
public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode) {
     this.activityResultCallback = command;
     this.activityResultKeepRunning = this.keepRunning;

     if (command != null) {
         this.keepRunning = false;
     }

     super.startActivityForResult(intent, requestCode);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
     super.onActivityResult(requestCode, resultCode, intent);
     CordovaPlugin callback = this.activityResultCallback;
     if (callback != null) {
         callback.onActivityResult(requestCode, resultCode, intent);
     }
} }

xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.cordovatest.CordovaTestActivity" >

<org.apache.cordova.CordovaWebView
 android:id="@+id/tutorialView"
 android:layout_width="match_parent"
 android:layout_height="match_parent" />

</RelativeLayout>

config.xml

<widget id="com.example.hello" version="0.0.1" xmlns="http://www.w3.org/ns/widgets" xmlns:cdv="http://cordova.apache.org/ns/1.0">
<preference name="loglevel" value="DEBUG" />
<preference name="AndroidLaunchMode" value="singleTop" />
<name>Hello World</name>
<description>
    A sample Apache Cordova application that responds to the deviceready event.
</description>
<author email="dev@cordova.apache.org" href="http://cordova.io">
    Apache Cordova Team
</author>
<content src="index.html" />
<access origin="*" />
<preference name="useBrowserHistory" value="true" />
<preference name="exit-on-suspend" value="false" />
<preference name="fullscreen" value="false" />
<preference name="webviewbounce" value="true" />
</widget>

【问题讨论】:

  • 看来cordova 的人在没有更新文档的情况下将 org.apache.cordova.CordovaWebView 类变成了一个界面。我会对您在撰写本文后发现的任何修复感兴趣。
  • @BrillPappin 在this related question 下列出了一个潜在的修复程序。但是,我无法让它正常工作。
  • @OJK - 谢谢,问题已经得到解决,我不再需要处理 Cordova,但我相信该链接会对其他人有所帮助。

标签: android cordova cordovawebview


【解决方案1】:

我知道我回答这个问题为时已晚,但今天我面临这个问题并试图找到解决方案。

在Android 5.1.1 cordova implementation 中,CordovaWebView 从 CustomwebView 转换为 interface,我们不能在布局中使用它,这是应用程序无法编译的原因。

cordova 提供的另一个 webview 实现称为 org.apache.cordova.engine 包下的 SystemWebView。

不知道为什么cordova开发者把cordovawebview转成界面

【讨论】:

  • 因为cordova android 4 包含了“可插入的webviews”,而这种改变是必要的。我不清楚这是答案还是评论
【解决方案2】:

我已经找到了解决方案。我们可以使用 org.apache.cordova.engine.SystemWebView 代替 CordovaWebView,它会按预期工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-12
    • 1970-01-01
    相关资源
    最近更新 更多