无法将自定义MyWebView与main.xml链接

时间:2011-07-19 06:32:25

标签: android-webview

我试图在我的应用程序中扩展WebView类,以便我可以检测滑动:

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)); 
    }
};

在我的main.xml文件中,我引用了MyWebView,如下所示:

    <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

并且应用程序在加载时中止。 Layout Inflater错误似乎指向main.xml中XML条目的第一行,但我不明白我哪里出错了。有人可以帮忙吗? 谢谢, MIKES

1 个答案:

答案 0 :(得分:0)

我最终通过定义一个LinearLayout视图解决了这个问题,我想在我的XML中使用webview,然后在我的onCreate代码中手动生成webview:

    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");

浏览器定义为

public MyWebView browser;

靠近我的活动顶部。相应的main.xml包括:

    <LinearLayout
        android:layout_below="@+id/linearLayout1"
        android:id="@+id/webkitLinearLayout"
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:layout_weight="1" />

这正是我想要的,但缺点是我无法在XML图形布局中正确看到布局。