2010-08-09 98 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