【问题标题】:Webview not working?网络视图不工作?
【发布时间】:2010-08-09 14:59:16
【问题描述】:

我有以下方法...

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 编译错误,但 full 被定义为.. WebView full = (WebView)findViewById(R.id.webview); 有人知道为什么这不起作用吗?

谢谢。

【问题讨论】:

  • 是在“onCreate”中吗?如果它找不到它,那么你在错误的“上下文”中。您可以发布该方法的完整代码吗?比如设置内容视图的位置等。
  • ZOOM_PARAMS 变量指的是什么?

标签: android xml


【解决方案1】:

删除“这个”。完整变量用法的前缀。 看起来您正在尝试访问方法局部变量,而“this.”关键字用于访问成员变量。

这里是修改后的代码示例,对我来说编译和运行都很好。

    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",
                                                            "");
    }

}

【讨论】:

  • .. 如果您想初始化成员变量并稍后在代码中访问它,请将 WebView full = 更改为 full =
【解决方案2】:

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

【讨论】:

  • 抱歉,我不太明白如何将该解决方案应用于我的问题,或许您可以再解释一下?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-27
  • 1970-01-01
  • 1970-01-01
  • 2015-03-16
相关资源
最近更新 更多