5

I've got some problems with Cordova 5.1.1 implementation on Android. I'm trying to embed a CordovaWebView in my application. I've followed all the instructions described in their official site, however I keep getting the error "org.apache.cordova.CordovaWebView cannot be cast to android.view.View". I've searched a lot for some help, but I couldn't find anything that could help me. Here's my code:

Activity:

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>
f_puras
  • 2,521
  • 4
  • 33
  • 38
  • 1
    It seems the cordova folks went and turned the org.apache.cordova.CordovaWebView class into an interface without updating the documentation. I'd be interested in any fix you found since this was written. – Brill Pappin Oct 06 '15 at 03:05
  • @BrillPappin there's a potential fix listed under [this related question](http://stackoverflow.com/questions/30489176/cordova-webview-inside-android-fragment-on-cordova-4-0). However, I have not been able to get it working properly. – OJ7 Mar 28 '16 at 14:36
  • @OJK - thanks, the problem has been longer resolved and I no longer have to deal with Cordova, but I'm sure the link will be helpful to others. – Brill Pappin Mar 29 '16 at 21:08

2 Answers2

1

I know that I am too late answer this question but today I face this issue and try to find solution .

In 5.1.1 cordova implementation for Android, CordovaWebView converted from CustomwebView to interface and we can not use it in layout, this the reason application can't compile.

Another webview implementation provided by cordova know as SystemWebView under org.apache.cordova.engine package.

I don't know why cordova developer's converted cordovawebview to interface

USKMobility
  • 5,721
  • 2
  • 27
  • 34
  • 1
    Because cordova android 4 included the "plugable webviews" and that change was necessary. It's not clear to me if this is an answer or just a comment – jcesarmobile Feb 24 '16 at 14:01
1

I have found a solution for this. We can use org.apache.cordova.engine.SystemWebView instead of CordovaWebView and it will work as expected.

Surendra Kumar
  • 2,787
  • 1
  • 12
  • 10