将自定义 MyWebView 与 main.xml 链接时遇到问题

发布于 2024-11-25 03:03:54 字数 5485 浏览 1 评论 0原文

我尝试在应用程序中扩展 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" />

我相信 mikes.BookOne.BookOne.MyWebView 是新类类型的完全限定路径。

不幸的是,当我尝试运行 LogCat 窗口时,出现错误:

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 条目的第一行,但我不明白哪里出了问题。有人可以帮忙吗? 谢谢, 麦克斯

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隔岸观火 2024-12-02 03:03:54

我最终通过定义一个 LinearLayout 视图解决了这个问题,我希望在 XML 中包含 web 视图,然后在 onCreate 代码中手动生成 web 视图:

    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 图形布局中正确看到布局。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文