ClassCastError将按钮放在webview顶部时出现异常

时间:2012-10-19 09:47:48

标签: android button webview classcastexception

我想在webview上放一个按钮。为此,我将XML定义为:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <WebView
        android:id="@+id/webview_news_fullscreen"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <Button
        android:id="@+id/done_btn_news_fullscreen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:background="@drawable/done_btn" />

</RelativeLayout>

活动类:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.article_full_screen);
    url = getIntent().getExtras().getString(Universal.TAG_PERMALINK);
    done_button = (Button)findViewById(R.id.done_btn_news_fullscreen);
    done_button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });

    webView = (WebView)findViewById(R.id.webview_news_fullscreen);
    webView.loadUrl(url);
}

但是,每次运行我的代码时,它都会崩溃并出现“ java.lang.ClassCastException:android.webkit.WebView ”错误。请帮帮我!

logcat的:

10-19 15:08:52.683: E/AndroidRuntime(845): FATAL EXCEPTION: main
10-19 15:08:52.683: E/AndroidRuntime(845): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.newsreaderui/com.example.newsreaderui.fullarticle.ArticleFullScreen}: java.lang.ClassCastException: android.webkit.WebView
10-19 15:08:52.683: E/AndroidRuntime(845):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-19 15:08:52.683: E/AndroidRuntime(845):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-19 15:08:52.683: E/AndroidRuntime(845):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-19 15:08:52.683: E/AndroidRuntime(845):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-19 15:08:52.683: E/AndroidRuntime(845):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-19 15:08:52.683: E/AndroidRuntime(845):  at android.os.Looper.loop(Looper.java:123)
10-19 15:08:52.683: E/AndroidRuntime(845):  at android.app.ActivityThread.main(ActivityThread.java:4627)
10-19 15:08:52.683: E/AndroidRuntime(845):  at java.lang.reflect.Method.invokeNative(Native Method)
10-19 15:08:52.683: E/AndroidRuntime(845):  at java.lang.reflect.Method.invoke(Method.java:521)
10-19 15:08:52.683: E/AndroidRuntime(845):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-19 15:08:52.683: E/AndroidRuntime(845):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-19 15:08:52.683: E/AndroidRuntime(845):  at dalvik.system.NativeStart.main(Native Method)
10-19 15:08:52.683: E/AndroidRuntime(845): Caused by: java.lang.ClassCastException: android.webkit.WebView
10-19 15:08:52.683: E/AndroidRuntime(845):  at com.example.newsreaderui.fullarticle.ArticleFullScreen.onCreate(ArticleFullScreen.java:26)
10-19 15:08:52.683: E/AndroidRuntime(845):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-19 15:08:52.683: E/AndroidRuntime(845):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
10-19 15:08:52.683: E/AndroidRuntime(845):  ... 11 more

2 个答案:

答案 0 :(得分:3)

尝试清理并重建项目。可能是愚蠢的回答,但是生成R.java文件存在问题。

答案 1 :(得分:1)

Eclipse - &gt;项目 - &gt;干净应该有帮助