Android的 - 扩展的WebView类的初始化引起ClassCastException异常 [英] Android - Extended WebView class causing ClassCastException on Initialization
问题描述
我的问题是真的直线前进。我似乎无法弄清楚它为什么抛出这样的错误(添加,我从来没有真正使用Java,但现在有一个简单的Android应用程序)。
My problem is really straight forward. I can't seem to figure out why it's throwing an error of this kind (to add, I've never really used Java but have to for a simple android app now).
在我的活动我班声明:
private MyWebView web;
在'的onCreate方法,我这样做:
Inside the 'onCreate' method I do this:
try {
web = (MyWebView)findViewById(R.id.webview);
}
catch(Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
}
类MyWebView看起来是这样的:
The class 'MyWebView' looks like this:
import android.app.AlertDialog;
import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebView;
public class MyWebView extends WebView {
public MyWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
@Override
public void onScrollChanged(int l, int t, int oldl, int oldt) {
}
}
这引发了ClassCastException异常与detailMessage =android.webkit.WebView。
This throws an a 'ClassCastException' with detailMessage = "android.webkit.WebView.
我真的AP preciate一些帮助。
I'd really appreciate some help.
感谢
推荐答案
您必须命名您的自定义的WebView像这样在你的布局:
you have to name your custom WebView like this in your layout:
<yourpackagename.MyWebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
yourpackagename:是包至极的你decalred的名字你MyWebView类
yourpackagename: is the name of the package in wich you decalred your MyWebView Class
这篇关于Android的 - 扩展的WebView类的初始化引起ClassCastException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!