2014-01-23 54 views
0

我知道你以前會看到這個問題。我知道我有:-)。但是,仍然,梳理我發現的所有答案,我仍然得到膨脹的類錯誤。我的佈局有一個擴展的webView,因爲我希望能夠左右滑動以加載前一個和下一個html頁面。 我包含了整個佈局,因爲我發現大部分答案都是以抽象的方式處理webView,而不是作爲大圖的一部分。希望這個答案能夠幫助他人獲得更大的圖片。Android:錯誤充氣擴展類

底部的scrollview會包含按鈕作爲瀏覽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 
+0

後所有異常跟蹤,並MyWebView代碼和佈局 – Dimmerg

+0

添加這個'公共MyWebView(上下文的背景下){ 超(上下文); //這個構造函數用於以編程方式創建視圖 }構造函數也試過,你可以移動自定義web視圖來分離.java文件 – Raghunandan

+0

@ Dimmerg:包含所有跟蹤請求。 MyWebView代碼在發佈代碼部分中用以下代碼行開始:「class MyWebView extends WebView {」佈局部分從發佈的xml中的行開始:「

回答

1

MyWebView類是內部MainActivity類的。 Inflater沒有看到這個課程。使你的WebView在不同的.java文件中。

您也可以嘗試在您的佈局文件<com.example.test.MainActivity.MyWebView/>中使用該名稱,但我不確定。

+0

我相信代碼已經存在。查看擴展了webView類的發佈代碼。大約四行:「類MyWebView擴展WebView {」 –

+0

MyWebView類是MainActivity類的內部。 Inflater沒有看到這個課程。使你的WebView在不同的.java文件中。 –

+0

您也可以嘗試在佈局文件中使用該名稱,但我不確定。 –

1
// 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> 
+0

感謝Haresh,我插入了你的代碼,但是這導致了同樣的錯誤。是否有可能是因爲我的佈局中的MyWebView不在最高層(在頂層的linearlayout內部)而是在相關佈局的內部下一層,所以應該調整MyWebView的路徑。像com.example.test.RelativeLayout.MyWebView? –

+0

您必須創建自定義WebView類而不是Activity內部類,因此您已在MainActivity中聲明爲內部類?然後爲Custom WebView創建單獨的類。 –

+0

感謝您使用示例代碼和大大改進的換頁機制。不幸的是,你並不是第一次知道該類被隱藏的實際解決方案。 –

0

請務必添加所有這些構造在自定義的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" 
       /> 
相關問題