2013-05-03 54 views
0

我曾尝试使用WebView从Google代码和其他资源中尝试示例,演示,但是当我尝试在自己的代码中执行代码时,它不适用于我。加载html + android中的错误

我要加载的index.html,我把资产的文件夹,并使用:

public class MainActivity extends Activity { 

    private WebView webView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 


     //define webview 
     webView = (WebView)findViewById(R.id.webView); 
     webView.setHorizontalScrollBarEnabled(false); 
     webView.loadUrl("file:///android_asset/www/index.html"); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

在模拟器我看不到我的档案还在它的白色背景,而不是打开我的index.html。

注: -

我的index.html是包含JavaScript比阿贾克斯获得来自网站的数据线上。

有什么方法可以在适用于所有API版本的应用程序包中加载现有的.html文件?

非常感谢您的帮助!


编辑: -

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    webView = (WebView) findViewById(R.id.webView); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.setWebChromeClient(new WebChromeClient() { 
      public void onProgressChanged(WebView view, int progress) { 
      MainActivity.this.setProgress(progress * 1000); 
      } 
     }); 
     webView.setWebViewClient(new WebViewClient() { 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
      Toast.makeText(MainActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     webView.loadUrl("file:///android_asset/www/index.html"); 
} 

我会尝试这个代码,但我有错误: -

Multiple markers at this line 
    - WebViewClient cannot be resolved to a type 
    - The method setWebViewClient(WebViewClient) in the type WebView is not applicable for the arguments (new 
    WebViewClient(){}) 
+0

如果你把一些文本放到你的html中会发生什么?它显示出来了吗? – Axarydax 2013-05-03 16:11:45

+0

@Axarydax不,我看不到它 – 2013-05-03 16:16:38

+0

http://stackoverflow.com/questions/16152491/enable-back-button-in-webview/16152530#16152530检查编辑2在工作示例的链接 – Raghunandan 2013-05-03 16:46:22

回答

1

如果您index.html包括JavaScript代码,请尝试:

WebSettings webSettings = webView.getSettings(); 
webSettings.setJavaScriptEnabled(true); 
+0

我会尝试但此代码有错误: - 'webSettings.setJavaScriptEnabled (true);' 它需要的东西! – 2013-05-03 18:10:02

+0

@kammaik你试过'webView.getSettings();'?您可以尝试:'webView.getSettings()。setJavaScriptEnabled(true);' – mmBs 2013-05-03 18:26:47

+0

是的,我这样做,但仍然显示错误 – 2013-05-03 20:45:52