我想在我的 WebView 应用程序中实现滑动功能,并在滑动时调用 HTML/Javascript 应用程序的 Javascript 中的函数。
根据这个问题的答案:How to detect a swipe gesture on webview
我做了这样的事情:
package (...);
import (...)
public class MainActivity extends Activity {
WebView mywebview;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebview = (WebView) findViewById(R.id.webview);
WebSettings webSettings = mywebview.getSettings();
mywebview.addJavascriptInterface(new WebAppInterface(this), "Android");
webSettings.setJavaScriptEnabled(true);
webSettings.setDatabaseEnabled(true);
webSettings.setDatabasePath("/data/data/" + this.getPackageName() + "/databases/");
webSettings.setDomStorageEnabled(true);
mywebview.setWebChromeClient(new WebChromeClient());
mywebview.setWebViewClient(new WebViewClient());
mywebview.loadUrl("file:///android_asset/www/index.html");
mywebview.setGestureDetector(new GestureDetector(new CustomeGestureDetector()));
}
private class CustomeGestureDetector extends SimpleOnGestureListener {
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if(e1 == null || e2 == null) return false;
if(e1.getPointerCount() > 1 || e2.getPointerCount() > 1) return false;
else {
try { // right to left swipe .. go to next page
if(e1.getX() - e2.getX() > 100 && Math.abs(velocityX) > 800) {
mywebview.loadUrl("javascript:left()");
return true;
} //left to right swipe .. go to prev page
else if (e2.getX() - e1.getX() > 100 && Math.abs(velocityX) > 800) {
mywebview.loadUrl("javascript:right()");
return true;
} //bottom to top, go to next document
else if(e1.getY() - e2.getY() > 100 && Math.abs(velocityY) > 800
&& mywebview.getScrollY() >= mywebview.getScale() * (mywebview.getContentHeight() - mywebview.getHeight())) {
mywebview.loadUrl("javascript:up()");
return true;
} //top to bottom, go to prev document
else if (e2.getY() - e1.getY() > 100 && Math.abs(velocityY) > 800 ) {
mywebview.loadUrl("javascript:down()");
return true;
}
} catch (Exception e) { // nothing
}
return false;
}
}
}
public final class CustomWebView extends WebView {
private GestureDetector gestureDetector;
/**
* @param context
* @param attrs
* @param defStyle
*/
public CustomWebView(Context context) {
super(context);
}
/**
* @param context
* @param attrs
* @param defStyle
*/
public CustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/**
* @param context
* @param attrs
* @param defStyle
*/
public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/*
* @see android.webkit.WebView#onScrollChanged(int, int, int, int)
*/
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
}
/*
* @see android.webkit.WebView#onTouchEvent(android.view.MotionEvent)
*/
@Override
public boolean onTouchEvent(MotionEvent ev) {
return gestureDetector.onTouchEvent(ev) || super.onTouchEvent(ev);
}
public void setGestureDetector(GestureDetector gestureDetector) {
this.gestureDetector = gestureDetector;
}
}
public class WebAppInterface {
Context mContext;
WebAppInterface(Context c) {
mContext = c;
}
}
但是当我启动应用程序时,控制台中有很多错误并且应用程序被关闭。那么我的代码有什么问题?
这是日志:
03-13 12:09:19.944: E/AndroidRuntime(311): FATAL EXCEPTION: main
03-13 12:09:19.944: E/AndroidRuntime(311): java.lang.RuntimeException: Unable to start activity ComponentInfo{packagename/packagename.ActivityName}: java.lang.ClassCastException: android.webkit.WebView
03-13 12:09:19.944: E/AndroidRuntime(311): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-13 12:09:19.944: E/AndroidRuntime(311): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-13 12:09:19.944: E/AndroidRuntime(311): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-13 12:09:19.944: E/AndroidRuntime(311): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-13 12:09:19.944: E/AndroidRuntime(311): at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 12:09:19.944: E/AndroidRuntime(311): at android.os.Looper.loop(Looper.java:123)
03-13 12:09:19.944: E/AndroidRuntime(311): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-13 12:09:19.944: E/AndroidRuntime(311): at java.lang.reflect.Method.invokeNative(Native Method)
03-13 12:09:19.944: E/AndroidRuntime(311): at java.lang.reflect.Method.invoke(Method.java:521)
03-13 12:09:19.944: E/AndroidRuntime(311): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-13 12:09:19.944: E/AndroidRuntime(311): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-13 12:09:19.944: E/AndroidRuntime(311): at dalvik.system.NativeStart.main(Native Method)
03-13 12:09:19.944: E/AndroidRuntime(311): Caused by: java.lang.ClassCastException: android.webkit.WebView
03-13 12:09:19.944: E/AndroidRuntime(311): at packagename.ActivityName.onCreate(ActivityName.java:47)
03-13 12:09:19.944: E/AndroidRuntime(311): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-13 12:09:19.944: E/AndroidRuntime(311): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-13 12:09:19.944: E/AndroidRuntime(311): ... 11 more
我的 XML 文件 (activity.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>