我有一個基本的WebView
應用程序,應該顯示www.google.com。儘管可以通過內置瀏覽器訪問該網站,但我無法從WebView
這樣做。我在各種論壇上經歷了大量的問題,並納入了所有建議,但似乎沒有任何成效。我已確保:
1.允許訪問互聯網的uses-permission
標籤是manifest
標籤的子標籤。
2.爲WebView啓用javascript。
清單文件:WebView頁面不可用 - Android
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sriram.hellowebview"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".helloWebview"
android:label="@string/title_activity_hello_webview" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
佈局:
<?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/helloWebview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
代碼:
/* Program to create sample webview.
* Steps:
* 1. Create webview.
* 2. Show some website in it.
* 3. Show some transitions as well.
*/
package com.sriram.hellowebview;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.webkit.WebView;
public class helloWebview extends Activity {
WebView myWebview;
String url = "www.google.com";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_webview);
Log.v(this.toString(), "Starting activity.");
myWebview = (WebView) findViewById(R.id.helloWebview);
Log.v(this.toString(), "Getting settings.");
myWebview.getSettings().setJavaScriptEnabled(true);
Log.v(this.toString(), "Loading URL now.");
myWebview.loadUrl(url);
Log.v(this.toString(), "Loaded URL.");
//open all links within the same webview.
//myWebview.setWebViewClient(new WebViewClient());
//Log.v(this.toString(), "All done here.");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_hello_webview, menu);
return true;
}
}
添加尾隨斜線。爲什麼這樣做的原因和其他一切失敗的原因? – Sriram 2013-03-26 08:33:43
@Sriram檢查此鏈接http://developer.android.com/guide/webapps/webview.html, – Janmejoy 2013-03-26 08:38:04