如何正确扩展WebView?

时间:2012-12-27 13:12:54

标签: android webview android-viewpager

我正在使用Scroll webview horizontally inside a ViewPager

的解决方案

它看起来非常有用,我在xml中引用了ExtendedWebView,

<ExtendedWebView
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

.class中的ExtendedWebView实例,

LayoutInflater mInflater =  (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout  = mInflater.inflate(R.layout.lay1, null);
ExtendedWebView webView = (ExtendedWebView)layout.findViewById(R.id.webview);

但是我得到了'错误膨胀类ExtendedWebView',所以有人可以帮我提一些想法,谢谢你的时间和经验!

继承人是完整的堆栈跟踪,

12-27 22:14:44.263: E/AndroidRuntime(10824): FATAL EXCEPTION: main
12-27 22:14:44.263: E/AndroidRuntime(10824): android.view.InflateException: Binary XML file line #7: Error inflating class com.vv_package_name.ExtendedWebView
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at com.vv_package_name.vv_main$ExtendedPagerAdapter.instantiateItem(vv_main.java:135)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:801)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.support.v4.view.ViewPager.populate(ViewPager.java:930)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.support.v4.view.ViewPager.populate(ViewPager.java:881)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1366)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.view.View.measure(View.java:8226)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.view.View.measure(View.java:8226)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.view.View.measure(View.java:8226)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.view.View.measure(View.java:8226)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.view.View.measure(View.java:8226)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.os.Looper.loop(Looper.java:123)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.app.ActivityThread.main(ActivityThread.java:4627)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at java.lang.reflect.Method.invokeNative(Native Method)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at java.lang.reflect.Method.invoke(Method.java:521)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at dalvik.system.NativeStart.main(Native Method)
12-27 22:14:44.263: E/AndroidRuntime(10824): Caused by: java.lang.ClassNotFoundException: com.vv_package_name.ExtendedWebView in loader dalvik.system.PathClassLoader[/data/app/com.vv_package_name-2.apk]
12-27 22:14:44.263: E/AndroidRuntime(10824):    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.view.LayoutInflater.createView(LayoutInflater.java:466)
12-27 22:14:44.263: E/AndroidRuntime(10824):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
12-27 22:14:44.263: E/AndroidRuntime(10824):    ... 33 more

如果使用WebView而不是ExtendedWebView,我的代码工作正常:)

4 个答案:

答案 0 :(得分:6)

您需要提供对该课程的完整参考,包括该课程。

例如,

<org.example.myapp.ExtendedWebView
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
 />

答案 1 :(得分:1)

抱歉,我是一个菜鸟,这是我的问题,我将这些扩展的webview代码放在一个.class文件中的主Activity类中...它应该用一个独立的.class文件保存......:)

public class ExtendedWebView extends WebView {

答案 2 :(得分:1)

在我的XML中使用它解决了我:

 <view
    class="com.example.MainActivity$MyWebView"
    android:id="@+id/web_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

答案 3 :(得分:-1)

这对我有用:

MainActivity.java:

package com.example.test;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.webkit.WebView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView webView = (WebView)findViewById(R.id.webView);
        webView.loadUrl("file:///android_asset/www/index.html");        
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

activity_main.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=".MainActivity" >

    <WebView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/webView" />

</RelativeLayout>

的index.html:

Hello world
<input type=text name=test value="some value" />
<button />