Having trouble linking custom MyWebView with main.xml
I have attempted to extend the WebView class in my application so that I can detect swipes:
public class BookOne extends Activity {
public static final String LOG_TAG = "BookOne";
private MyWebView browser;
private WebView listing_area;
private GestureDetector detector;
public class MyWebView extends WebView {
GestureDetector gd;
public MyWebView(Context context, AttributeSet attributes) {
super(context, attributes);
this.
gd = new GestureDetector(new SwipeDetector());
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.v(LOG_TAG, "onTouchEvent 2");
return (gd.onTouchEvent(event) || super.onTouchEvent(event));
}
};
In my main.xml file I've referenced MyWebView as follows:
<mikes.BookOne.BookOne.MyWebView android:layout_below="@+id/linearLayout1" android:id="@+id/webkit" android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1" />
where I believe that mikes.BookOne.BookOne.MyWebView is the fully qualified path to the new class type.
Unfortunately, I'm getting errors in the LogCat window when I try to run it:
07-18 23:14:33.630: ERROR/AndroidRuntime(22630): FATAL EXCEPTION: main 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): java.lang.RuntimeException: Unable to start activity ComponentInfo{mikes.BookOne/mikes.BookOne.BookOne}:android.view.InflateException: Binary XML file line #62: Error inflating class mikes.BookOne.BookOne.MyWebView 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1831) 07-18 23:14:33.630: ERROR/AndroidRuntime(开发者_如何学运维22630): at android.app.ActivityThread.access$500(ActivityThread.java:122) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1024) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at android.os.Handler.dispatchMessage(Handler.java:99) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at android.os.Looper.loop(Looper.java:132) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at android.app.ActivityThread.main(ActivityThread.java:4123) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at java.lang.reflect.Method.invokeNative(Native Method) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at java.lang.reflect.Method.invoke(Method.java:491) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at dalvik.system.NativeStart.main(Native Method) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): Caused by: android.view.InflateException: Binary XML file line #62: Error inflating class mikes.BookOne.BookOne.MyWebView 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:682) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at android.view.LayoutInflater.rInflate(LayoutInflater.java:724) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at android.view.LayoutInflater.rInflate(LayoutInflater.java:727) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at android.view.LayoutInflater.inflate(LayoutInflater.java:479) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at android.view.LayoutInflater.inflate(LayoutInflater.java:391) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at android.view.LayoutInflater.inflate(LayoutInflater.java:347) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:223) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at android.app.Activity.setContentView(Activity.java:1786) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at mikes.BookOne.BookOne.onCreate(BookOne.java:192) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at android.app.Activity.performCreate(Activity.java:4397) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1779) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): ... 11 more 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): Caused by: java.lang.ClassNotFoundException: mikes.BookOne.BookOne.MyWebView in loader dalvik.system.PathClassLoader[/data/app/mikes.BookOne-1.apk] 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:251) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at java.lang.ClassLoader.loadClass(ClassLoader.java:540) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at java.lang.ClassLoader.loadClass(ClassLoader.java:500) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at android.view.LayoutInflater.createView(LayoutInflater.java:542) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:671) 07-18 23:14:33.630: ERROR/AndroidRuntime(22630): ... 22 more
and the application aborts on load. The Layout Inflater errors appear to point to the first line of the XML entry in main.xml, but I don't understand where I've gone wrong. Can anyone help? Thanks, MikeS
I eventually worked around this problem by defining a LinearLayout view where I wanted the webview in my XML, and then manually generating the webview in my onCreate code:
ViewGroup wkll = (ViewGroup) findViewById(R.id.webkitLinearLayout);
LinearLayout.LayoutParams tlp =
new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT );
browser = new MyWebView(this);
wkll.addView(browser);
browser.setLayoutParams(tlp);
browser.getSettings().setDefaultFontSize(24);
browser.getSettings().setLoadsImagesAutomatically(true);
browser.getSettings().setSaveFormData(true);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setSupportMultipleWindows(true);
browser.loadUrl("file:///android_asset/cover.html");
browser was defined as
public MyWebView browser;
near the top of my activity. And the corresponding main.xml includes:
<LinearLayout
android:layout_below="@+id/linearLayout1"
android:id="@+id/webkitLinearLayout"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="1" />
This does exactly what I want, with the downside that I can't see the layout properly in the XML graphical layout.
精彩评论