【问题标题】:Android: error inflating extended classAndroid:错误膨胀扩展类
【发布时间】:2014-01-23 08:39:41
【问题描述】:

我知道你以前会看到这个问题。我知道我有:-)。但是,结合我发现的所有答案,我仍然得到膨胀类错误。我的布局有一个扩展的 webView,因为我希望能够左右滑动以加载上一个和下一个 html 页面。 我包括了整个布局,因为我发现我发现的大多数答案都以某种抽象的方式提出了一个解决方案,它只处理 webView 而不是更大图片的一部分。希望当这个问题得到解答时,它也能帮助其他人获得更大的图景。

底部的滚动视图将包含按钮作为导航 html 页面的替代方式。视图 imageView2 将包含一个透明的标头,部分遮盖了底层的 webView。 textView helloText 将显示章节标题。在我不得不扩展 webView 以处理左右滑动之前,我已经完成了所有这些工作。

这是布局xml:

<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.8"
        android:orientation="vertical" >

        <com.example.test.MyWebView
            android:id="@+id/webView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" />

        <View
            android:id="@+id/imageView2"
            android:layout_width="fill_parent"
            android:layout_height="50dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" 
            android:background="#88FF0000"/>
    </RelativeLayout>

    <TextView
        android:id="@+id/helloText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.05"
        android:gravity="center_horizontal"
        android:text="Hello World" />

    <HorizontalScrollView
        android:id="@+id/horizontalScrollView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.15"
            android:background="#0000FF" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >

             <Button
            android:id="@+id/button1"
            android:layout_width="110dp"
            android:layout_height="45dp"
            android:background="#00FF00" />

        </LinearLayout>
    </HorizontalScrollView>

</LinearLayout>

这是代码:

package com.example.test;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MotionEvent;
import android.webkit.WebView;
import android.widget.Toast;

public class MainActivity extends Activity {

int currentPage = 2; // page number for current html file

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //get a pointer to the webview
    MyWebView webview = (MyWebView) findViewById(R.id.webView1);

    webview.getSettings().setUseWideViewPort(true); 
    webview.getSettings().setSupportZoom(true);
    webview.getSettings().setBuiltInZoomControls(true);

    // put html page in the webView
    webview.loadUrl("file:///android_asset/chapter" + currentPage + ".html");
}

public void changePage(int page) {

    Log.v("test", "change page to " + page);

    //get a pointer to the webview
    MyWebView webview = (MyWebView)findViewById(R.id.webView1);

    // put html page in the webView
    webview.loadUrl("file:///android_asset/chapter" + page + ".html");

    }

class MyWebView extends WebView {
     Context context;
     GestureDetector gd;

    public MyWebView(Context context, AttributeSet attrs) {
    super(context, attrs);

    this.context = context;
         gd = new GestureDetector(context, sogl);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return (gd.onTouchEvent(event) 
                || super.onTouchEvent(event));
    }

     GestureDetector.SimpleOnGestureListener sogl = new GestureDetector.SimpleOnGestureListener() {
      public boolean onDown(MotionEvent event) {
       return false;
      }
      public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
       if (event1.getRawX() > event2.getRawX() && StrictMath.abs(event1.getRawY()-event2.getRawY())<100) {
        show_toast("swipe left");
        currentPage += 1;
        if (currentPage >= 19) currentPage = 19;
        changePage(currentPage);
       } else if(event1.getRawX() < event2.getRawX() && StrictMath.abs(event1.getRawY()-event2.getRawY())<100){
        show_toast("swipe right");
        currentPage -= 1;
        if (currentPage <= 1) currentPage = 1;
        changePage(currentPage);
       } else {
         return false;
       }
    return true;
      }
     };

     void show_toast(final String text) {
      Toast t = Toast.makeText(context, text, Toast.LENGTH_SHORT);
      t.show();
     }
    }
}

实际错误如下:

java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.test/com.example.test.MainActivity}:android.view.InflateException:二进制 XML 文件第 15 行:膨胀类 com.example 时出错。 test.MyWebView

提前感谢您的帮助。非常感谢。

先生。夹具

根据要求提供完整的错误跟踪:

01-23 10:21:51.980: E/AndroidRuntime(29514): FATAL EXCEPTION: main
01-23 10:21:51.980: E/AndroidRuntime(29514): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: android.view.InflateException: Binary XML file line #15: Error inflating class com.example.test.MyWebView
01-23 10:21:51.980: E/AndroidRuntime(29514):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at android.os.Looper.loop(Looper.java:137)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at android.app.ActivityThread.main(ActivityThread.java:4898)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at java.lang.reflect.Method.invokeNative(Native Method)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at java.lang.reflect.Method.invoke(Method.java:511)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at dalvik.system.NativeStart.main(Native Method)
01-23 10:21:51.980: E/AndroidRuntime(29514): Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class com.example.test.MyWebView
01-23 10:21:51.980: E/AndroidRuntime(29514):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:308)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at android.app.Activity.setContentView(Activity.java:1924)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at com.example.test.MainActivity.onCreate(MainActivity.java:21)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at android.app.Activity.performCreate(Activity.java:5206)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
01-23 10:21:51.980: E/AndroidRuntime(29514):    ... 11 more
01-23 10:21:51.980: E/AndroidRuntime(29514): Caused by: java.lang.ClassNotFoundException: com.example.test.MyWebView
01-23 10:21:51.980: E/AndroidRuntime(29514):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at android.view.LayoutInflater.createView(LayoutInflater.java:552)
01-23 10:21:51.980: E/AndroidRuntime(29514):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
01-23 10:21:51.980: E/AndroidRuntime(29514):    ... 22 more

【问题讨论】:

  • 发布所有异常跟踪,以及 MyWebView 代码和布局
  • 也添加这个public MyWebView(Context context) { super(context); // this constructor used when programmatically creating view } 构造函数并尝试将自定义Web 视图移动到单独的.java 文件中
  • @Dimmerg:按要求包括所有跟踪。 MyWebView 代码从发布的代码部分开始,行:“class MyWebView extends WebView {”布局部分从发布的 xml 中的“
  • @Raghunandan:我相信代码已经存在。请参阅扩展 webView 类的已发布代码。从以下大约四行:“class MyWebView extends WebView {”
  • @Mr.Jigs 将其移动到独立文件并按照建议添加另一个构造函数并尝试

标签: android android-layout webview inflate-exception


【解决方案1】:

确保在您的自定义 webview 类中添加所有这些构造函数

    public CustomWebView(Context context) {
        super(context);
    }

    public CustomWebView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomWebView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public CustomWebView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

并且不要忘记在 xml 布局文件中使用这个 CustomWebView 类和你的完整的包名

 <com.kittykitty.meowmeow.CustomWebView
                android:id="@+id/my_webview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                />

【讨论】:

    【解决方案2】:

    MyWebView 类是 MainActivity 类的内部。 Inflater 没有看到这个类。在不同的 .java 文件中制作您的 WebView。

    您也可以尝试在布局文件中使用该名称&lt;com.example.test.MainActivity.MyWebView/&gt;,但我不确定。

    【讨论】:

    • 我相信该代码已经存在。请参阅扩展 webView 类的已发布代码。从以下大约四行:“class MyWebView extends WebView {”
    • MyWebView 类是 MainActivity 类的内部。 Inflater 没有看到这个类。在不同的 .java 文件中创建您的 WebView。
    • 您也可以尝试在布局文件 中使用该名称,但我不确定。
    • 是的,你是对的。更重要的是,您实际上解释了原因。这是一个很大的帮助。谢谢你。如果我不能接受 Raghunadan 的评论广告正确答案,他确实先到了那里,我会接受。
    • 我已经更新了我的帖子。此信息对于此问题更为实际。
    【解决方案3】:
    // try this way
    1. Custom WebView Class
    public class MyWebView extends WebView {
        Context context;
        GestureDetector gd;
        OnPageChangleListener listener;
        private int currentPage=1;
        private int startPageIndex;
        private int lastPageIndex;
    
        public MyWebView(Context context) {
            super(context);
           init(context);
        }
    
        public MyWebView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(context);
        }
    
        public MyWebView(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init(context);
        }
    
        private void init(Context context){
            this.context = context;
            gd = new GestureDetector(context, sogl);
        }
    
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            return (gd.onTouchEvent(event)
                    || super.onTouchEvent(event));
        }
    
        public void setOnPageChangeListener(OnPageChangleListener listener){
            this.listener = listener;
        }
    
        public void setStartPageIndex(int startPageIndex){
            this.startPageIndex = startPageIndex;
        }
    
        public int getStartPageIndex(){
            currentPage = startPageIndex;
            return startPageIndex;
        }
    
        public int getLastPageIndex(){
            return lastPageIndex;
        }
    
        public void setLastPageIndex(int lastPageIndex){
            this.lastPageIndex = lastPageIndex;
        }
        GestureDetector.SimpleOnGestureListener sogl = new GestureDetector.SimpleOnGestureListener() {
            public boolean onDown(MotionEvent event) {
                return false;
            }
            public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
                if (event1.getRawX() > event2.getRawX() && StrictMath.abs(event1.getRawY()-event2.getRawY())<100) {
                    currentPage += 1;
                    if (currentPage > lastPageIndex){
                        currentPage = lastPageIndex;
                        show_toast("Last Page");
                    }else{
                        show_toast("swipe left");
                        listener.onPageChange(currentPage);
                    }
                } else if(event1.getRawX() < event2.getRawX() && StrictMath.abs(event1.getRawY()-event2.getRawY())<100){
                    currentPage -= 1;
                    if (currentPage < startPageIndex) {
                        currentPage = startPageIndex;
                        show_toast("First Page");
                    }else{
                        show_toast("swipe right");
                        listener.onPageChange(currentPage);
                    }
                } else {
                    return false;
                }
                return true;
            }
        };
    
        void show_toast(final String text) {
            Toast t = Toast.makeText(context, text, Toast.LENGTH_SHORT);
            t.show();
        }
    
    }
    
    2. Custom Interface for you Custom WebView page change listener.
    public interface OnPageChangleListener {
        public void onPageChange(int pageNo);
    }
    
    3. Use Custom WebView in your Activity.
     MyWebView myWebView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            myWebView = (MyWebView) findViewById(R.id.myWebView);
            myWebView.getSettings().setUseWideViewPort(true);
            myWebView.getSettings().setSupportZoom(true);
            myWebView.getSettings().setBuiltInZoomControls(true);
            myWebView.setStartPageIndex(2);
            myWebView.setLastPageIndex(4);
            myWebView.setOnPageChangeListener(new OnPageChangleListener() {
                @Override
                public void onPageChange(int pageNo) {
                    System.out.println(String.valueOf(pageNo));
                    myWebView.loadUrl("file:///android_asset/" + pageNo + ".html");
                }
            });
    
            myWebView.loadUrl("file:///android_asset/" + myWebView.getStartPageIndex() + ".html");
    
    }
    4.main.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <com.example.Demo.MyWebView
            android:id="@+id/myWebView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
    

    【讨论】:

    • 感谢 Haresh,我已插入您的代码,但这会导致同样的错误。考虑到在我的布局中,MyWebView 不在最高级别(就在顶层的线性布局内),而是在相对布局内下降一级,是否应该调整 MyWebView 的路径。 com.example.test.RelativeLayout.MyWebView 之类的东西?
    • 您必须创建自定义 WebView 类而不是 Activity 内部类,所以您在 MainActivity 中声明为内部类?然后为自定义 WebView 创建单独的类。
    • 感谢您提供示例代码和大大改进的页面更改机制。不幸的是,您并不是第一个解决该类被 theMainActivity 隐藏的实际解决方案。
    猜你喜欢
    • 2011-04-13
    • 2015-09-18
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 2021-04-09
    • 2016-11-12
    • 2016-12-06
    • 1970-01-01
    相关资源
    最近更新 更多