Webview无法正常工作?

时间:2010-08-09 14:59:16

标签: android xml

我有以下方法......

public class Image extends Activity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);

/* Using WebView to display the full-screen image */
WebView full = (WebView)findViewById(R.id.webview);

/* Set up the Zoom controls */
FrameLayout mContentView = (FrameLayout) getWindow().
getDecorView().findViewById(android.R.id.content);
final View zoom = this.full.getZoomControls();
mContentView.addView(zoom, ZOOM_PARAMS);
zoom.setVisibility(View.VISIBLE);

/* Create a new Html that contains the full-screen image */
String html = new String();
html = ("<html><center><img src=\"1276253554007.jpg\"></html>" );

/* Finally, display the content using WebView */
full.loadDataWithBaseURL("file:///sdcard/DCIM/Camera/",
                                                        html,
                                                        "text/html",
                                                        "utf-8",
                                                        "");
}

}

R.id.webview引用...

 <?xml version="1.0" encoding="utf-8"?>
 <WebView  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>

但是我收到full cannot be resolved or is not a field编译错误,但是完整定义为.. WebView full = (WebView)findViewById(R.id.webview);任何人都知道为什么这不起作用?

感谢。

2 个答案:

答案 0 :(得分:2)

删除“这个”。来自完整变量用法的前缀。 看起来你正试图访问方法局部变量,而“这个”。 keyword用于访问成员变量。

这是修改后的代码示例,它编译并运行得很好。

    public class Image extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu);

    /* Using WebView to display the full-screen image */
    WebView full = (WebView)findViewById(R.id.webview);

    /* Set up the Zoom controls */
    FrameLayout mContentView = (FrameLayout) getWindow().
    getDecorView().findViewById(android.R.id.content);
    final View zoom = full.getZoomControls();
    mContentView.addView(zoom);
    zoom.setVisibility(View.VISIBLE);

    /* Create a new Html that contains the full-screen image */
    String html = new String();
    html = ("<html><center><img src=\"1276253554007.jpg\"></html>" );

    /* Finally, display the content using WebView */
    full.loadDataWithBaseURL("file:///sdcard/DCIM/Camera/",
                                                            html,
                                                            "text/html",
                                                            "utf-8",
                                                            "");
    }

}

答案 1 :(得分:1)

您对其他答案的评论让我想起similiar problem I once read on SO。猜猜它是一样的:你在Actitvity视图中寻找WebView,而我猜它实际上是在对话视图中声明的......链接的答案解决了其他OP的问题。