2013-03-26 81 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);