2012-11-27 79 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; 
      } 
     }); 
    } 
相关问题