2012-11-27 77 views
0

我寫了一個擴展了FrameLayout的類。新類擴展FrameLayout無法實例化

public class ReaderFrameLayout extends FrameLayout 
{ 
    public ReaderFrameLayout(Context context) 
    { 
     this(context, null); 
    } 

    public ReaderFrameLayout(Context context, AttributeSet attrs) 
    { 
     this(context, attrs, 0); 
    } 

    public ReaderFrameLayout(Context context, AttributeSet attrs, int defaultStyle) 
    { 
     super(context, attrs, defaultStyle); 
     WebView readerWebView = (WebView) findViewById(R.id.fragment_reader_webview); 
     readerWebView.setOnTouchListener(new OnTouchListener() 
     { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) 
      { 
       Log.d("ReaderFrameLayout", "setOnTouchListener"); 
       return true; 
      } 
     }); 
    } 
} 

並將其添加到片段中。

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

    <com.mbs.helpers.ReaderFrameLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="83dp" > 

     <WebView 
      android:id="@+id/fragment_reader_webview" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" /> 

    </com.mbs.helpers.ReaderFrameLayout> 

</RelativeLayout> 

在Eclipse中的錯誤:

com.android.layoutlib.bridge.MockView不能轉換到android.view.ViewGroup 異常詳細信息記錄在窗口>顯示視圖>錯誤日誌 的以下類無法實例化: - com.mbs.helpers.ReaderFrameLayout(Open Class,Show Error Log) 有關更多詳細信息,請參見錯誤日誌(窗口>顯示視圖)。 提示:在自定義視圖中使用View.isInEditMode()可以在Eclipse中顯示時跳過代碼

當然,該應用程序不起作用。

我在做什麼錯?

+0

你指的是關係到圖形佈局Eclipse的錯誤。這與設備上的操作無關。那麼解釋一下什麼是行不通的?它會拋出錯誤嗎?我看到的只是兩個佈局,沒有別的,如果你設置上面的佈局,它將是空的 – nandeesh

回答

0

只是編程創建Web視圖:

public ReaderFrameLayout(Context context, AttributeSet attrs, int defaultStyle) 
{ 
    super(context, attrs, defaultStyle); 
    WebView readerWebView = new WebView(context); 
    addView(readerWebView); 
    readerWebView.setOnTouchListener(new OnTouchListener() 
    { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) 
     { 
      Log.d("ReaderFrameLayout", "setOnTouchListener"); 
      return true; 
     } 
    }); 
} 

,並從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" > 

    <com.mbs.helpers.ReaderFrameLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginTop="83dp" /> 

</RelativeLayout> 

刪除它。如果你真的想創建一個XML佈局自定義視圖,那麼你需要在一個單獨的XML文件中做到這一點,並在您的自定義類中充氣。

對於instace,res/layout/reader.xml

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <WebView 
     android:id="@+id/fragment_reader_webview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

</FrameLayout> 

並在代碼:

public ReaderFrameLayout(Context context, AttributeSet attrs, 
      int defaultStyle) { 
     super(context, attrs, defaultStyle); 
     LayoutInflater.from(context).inflate(R.layout.reader, this); 
     WebView readerWebView = (WebView) findViewById(R.id.fragment_reader_webview); 
     readerWebView.setOnTouchListener(new OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       Log.d("ReaderFrameLayout", "setOnTouchListener"); 
       return true; 
      } 
     }); 
    } 
相關問題