2010-08-09 62 views
0

我有下面的方法...Webview不工作?

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 refrences ...

<?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);任何人都知道为什么这不工作?

谢谢。

+0

是在“onCreate”?如果它无法找到它,那么你是在错误的“背景”。你可以发布整个代码的方法?比如设置内容视图的位置等。 – 2010-08-09 15:03:51

+0

我提出了完整的方法。 – Skizit 2010-08-09 15:13:36

+0

要什么ZOOM_PARAMS变量引用? – 2010-08-09 15:36:53

回答

2

删除“this”。全变量用法的前缀。 看起来你试图访问方法局部变量,而“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", 
                  ""); 
    } 

} 
+0

..或者如果你想初始化一个成员变量并且稍后在代码中访问它,或者把'WebView full ='改成'full ='。 – 2010-08-09 15:05:42

+0

@Andreas_D这也会导致一个空指针。 – Skizit 2010-08-09 15:22:45

1

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

+0

对不起,但我并不真正了解如何将该解决方案应用于我的问题,或许您可以再解释一下? – Skizit 2010-08-09 15:48:31