【发布时间】:2012-02-03 03:32:54
【问题描述】:
我正在阅读 android 教程并尝试了 WebView 示例。这就是我最终得到的结果:
WebAppActivity
public class WebAppActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView wv = (WebView) findViewById(R.id.webView1);
wv.loadUrl("http://www.google.com");
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<WebView
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</WebView>
</LinearLayout>
但不是在应用程序本身中加载页面,而是在应用程序启动时打开默认的 android 浏览器,并且页面在浏览器而不是应用程序中加载。当我按下返回时,我返回到显示空白屏幕的应用程序活动。
有人知道为什么会这样吗?
编辑:
清单
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".WebAppActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
这只是为了表明我已经添加了 INTERNET 权限
编辑:
只要我添加了WebViewClient,
wv.setWebViewClient(new WebViewClient() {});
页面在应用程序中加载。这是预期的行为吗? Android WebView 需要 WebViewClient 吗? (找不到任何文档)
编辑:
我注意到当我在具有 Google API 的模拟器中安装 apk 时会出现此问题。在普通模拟器(没有 Google API)上,它的行为符合预期。
【问题讨论】:
-
您的问题已在以下位置得到解答:stackoverflow.com/questions/2378800/…
-
@silent 请仔细阅读。那不是我的问题。我在加载第一页本身时遇到问题,它在浏览器而不是应用程序中加载。见弗洛的评论
-
不,我认为他不是在谈论被点击的网页本身的链接。这听起来像网页根本没有加载到 WebView 中。
-
这里有一个详细的例子:Android - WebView client example,同意你@silent。
-
@PareshMayani 甚至没有加载第一页。我说的是链接点击。我尝试使用
webView.loadUrl("http://google.com")加载的第一页没有被加载。它直接调用浏览器。这是预期的行为吗?