2013-03-26 80 views
1

我有一個基本的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; 
    } 
} 

回答

6
String url = "https://www.google.co.in/"; 

或其他

super.loadUrl("https://www.google.co.in//"); 
+0

添加尾隨斜線。爲什麼這樣做的原因和其他一切失敗的原因? – Sriram 2013-03-26 08:33:43

+1

@Sriram檢查此鏈接http://developer.android.com/guide/webapps/webview.html, – Janmejoy 2013-03-26 08:38:04

4

嘗試添加的是http://你的鏈接前

3
WebView wt; 

wt.loadUrl("http://www.google.com/"); 

,有時在Android 4.0的將是一片空白,在清單文件中使用android:hardwareAccelerated="true"

+1

android:hardwareAccelerated =「true」爲我做了。如果您已經添加了Manifest權限並仍然存在問題,那麼這就是解決方案。謝謝! – AlexVPerl 2013-08-25 15:31:07

0

您需要添加http:// URL字符串...的URL必須看起來像這樣

字符串URL = 「http://www.google.com/」;

現在做

webview.loadurl(url);