【发布时间】:2013-05-03 16:10:16
【问题描述】:
我确实尝试过使用 WebView 的示例、来自 Google 代码的演示和其他资源,但是当我尝试在自己的代码中执行此操作时,它对我不起作用。
我想加载我放在 assets 文件夹中的 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,而不是通过 ajax 从网站在线获取数据。
有没有办法在适用于所有 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(){})
【问题讨论】:
-
如果将一些文本放入 html 会发生什么?它出现了吗?
-
@Axarydax 不,我看不到它
-
stackoverflow.com/questions/16152491/… 检查链接中的编辑 2 以获取工作示例
-
@Raghunandan 我会试试你的代码,但我有错误,请参阅我的更新帖子
-
有没有导入import android.webkit.WebViewClient;检查你的进口
标签: android