Android 自定义Webview上的ClassCastException
在onCreate中,我有:Android 自定义Webview上的ClassCastException,android,classcastexception,Android,Classcastexception,在onCreate中,我有: MyWebView webView = new MyWebView(this, null); webView = (MyWebView) findViewById(R.id.pdf); 交易类中的自定义webview类: public class MyWebView extends WebView { Paint p = new Paint(); public MyWebView(Context context, AttributeSet
MyWebView webView = new MyWebView(this, null);
webView = (MyWebView) findViewById(R.id.pdf);
交易类中的自定义webview类:
public class MyWebView extends WebView {
Paint p = new Paint();
public MyWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
p.setColor (Color.RED);
canvas.drawLine (20, 10, 300, 400, p);
}
}
XML:
<im.magic.dealpdf.Deal.MyWebView
android:id="@+id/pdf"
android:layout_weight="0.4"
android:layout_margin="5dp"
android:layout_width="0dp"
android:layout_height="match_parent"/>
该错误表示找不到类im.magic.dealpdf.Deal.MyWebView。为什么会出现错误?将xml更改为im.magic.dealpdf.Deal会导致无法转换为android.view.view
更新:我想我需要在我的Deal类中扩展视图。但是它已经扩展了活动您不应该以这种方式使用内部类,将MyWebView声明为
public static class MyWebView extends WebView {
...
}
否则,不可能在交易实例之外创建MyWebView实例。
在布局文件中不能以这种方式引用内部类,因为您必须编写如下内容:
<view class="im.magic.dealpdf.Deal@MyWebView"
android:id="@+id/pdf"
android:layout_weight="0.4"
android:layout_margin="5dp"
android:layout_width="0dp"
android:layout_height="match_parent"/>
尝试清理并重新构建可能有效的项目