0

我想在 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 ”错误。请帮帮我!

日志猫:

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
4

2 回答 2

3

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

于 2012-10-19T09:50:28.287 回答
1

Eclipse -> Project -> Clean 应该会有所帮助

于 2012-10-19T09:54:22.333 回答