我正在尝试将网页嵌入到 Android 应用程序中。 以下是我的应用程序的 main_activity java 文件中编写的代码。
package com.example.webpage;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webview = new WebView(this);
setContentView(webview);
webview.loadUrl("http://www.google.com/");
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
问题是每当我在模拟器中启动我的应用程序时,它都会显示“找不到页面”错误。 请帮帮我。
最佳答案
试试这个:
首先,获取 XML 格式的 Web View 。
像这样绑定(bind)到它们:
WebView webView = (WebView)findViewById(R.id.webView1);
然后执行以下操作:
String url = "http://www.google.com/"; WebView webView = (WebView)findViewById(R.id.webView1); webView.getSettings().setJavaScriptEnabled(true); webView.loadUrl(url);
关于java - 在 Android 应用程序中嵌入网页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26668982/