2013-01-20 69 views
1

我有一個演示文件在原始文件夾下res。當我點擊一個按鈕打開文件出現空白屏幕時,我無法解決問題。無法顯示html文件出現空白屏幕

下面的代碼:

XML文件:

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

對於活動:

package com.ashsoft.basiccprogram; 

import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
    import java.io.InputStream; 

    import android.app.Activity; 
    import android.os.Bundle; 
    import android.webkit.WebView; 

     public class First extends Activity{ 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.first); 


     WebView webView = (WebView) this.findViewById(R.id.webView1); 
     webView.getSettings().setBuiltInZoomControls(true); 
     webView.loadData(readTextFromResource(R.raw.demo), "text/html", "utf-8"); 
} 


    private String readTextFromResource(int resourceID) 
     { 
      InputStream raw = getResources().openRawResource(resourceID); 
      ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     int i; 
     try 
      { 
      i = raw.read(); 
       while (i != -1) 
      { 
     i = raw.read(); 
       } 
      raw.close(); 
      } 
      catch (IOException e) 
     { 
       e.printStackTrace(); 
     } 
      return stream.toString(); 
     } 

}; 

我得到一個空白屏幕。 demo.html保存在原始文件夾中。

回答

1

移動demo.html到您assets文件夾及用途:

webView.loadUrl("file:///android_asset/demo.html"); 
+0

謝謝。有效 ! 我們可以在webview上添加添加嗎?例如,我可以在webview上使用admob – ashwinbhy

+0

@ashwinbhy您不能在WebView AFAIK中使用admob本身,但您應該可以使用AdSense。或者您可以將AdMob放入您的活動中。 –