【问题标题】:New class extends FrameLayout could not be instantiated新类扩展 FrameLayout 无法实例化
【发布时间】:2012-11-27 10:31:21
【问题描述】:

我写了一个扩展 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 中显示时跳过代码

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

我做错了什么?

【问题讨论】:

  • 您所指的eclipse错误与图形布局有关。这与设备上的操作没有任何关系。那么请解释什么不起作用?它会抛出错误吗?我看到的只是两个布局,没有别的,如果你设置上面的布局,它将是空的

标签: android android-layout


【解决方案1】:

只需以编程方式创建 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;
            }
        });
    }

【讨论】:

    猜你喜欢
    • 2013-07-04
    • 2015-02-01
    • 1970-01-01
    • 2016-01-07
    • 2021-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-12
    相关资源
    最近更新 更多