无法实例化扩展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(打开类,显示错误日志)有关详细信息,请参阅错误日志(窗口>显示视图)。提示:在自定义视图中使用View.isInEditMode()可以跳过Eclipse中显示的代码

当然,该应用程序不起作用。

我做错了什么?

只需以编程方式创建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文件中进行创建,并在自定义类中进行扩展。

对于安装,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;
}
});
}

最新更新