WebView未打开网页,但浏览器已打开(错误:网页不可用)



我正在创建一个Android应用程序(Android 2.3.3(,它有一个页眉、一个页脚和一个WebView。问题是WebView没有打开任何网页。(注意:我正在模拟器上运行该应用程序(。

我尝试使用Android浏览器打开网页,网页打开正确。我也试过:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);

代码工作正常(在浏览器中打开页面(。

我一直在使用www.google.com和我自己的域名,我也一直在使用两个网页的ip地址(对于谷歌72.14.204.147,对于我自己,我自己的开发服务器的ip(。

此外,在编写最流行的答案之前,我已经在application标记之前有了<uses-permission android:name="android.permission.INTERNET" />

我在任何人要求之前添加代码:

活动java文件:

public class MyActivity extends Activity {
//Global Variables
WebView mainWebView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    loadActivityViews();
    mainWebView.getSettings().setJavaScriptEnabled(true);
    mainWebView.loadUrl("www.google.com");
    mainWebView.setWebViewClient(new MyWebViewClient());
}
/** Loads all global views of the Activity */
private void loadActivityViews(){
    mainWebView = (WebView) findViewById(R.id.index_main_web_view);
} 
//Internal Classes
/*
 * MyWebView Class
 * 
 * Forces links to open in the same webView
 * Handles the back button.
 * */
public class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
    @Override
    public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
        if ((event.getKeyCode() == KeyEvent.KEYCODE_BACK) && view.canGoBack()) {
            view.goBack();
            return true;
        }
        return super.shouldOverrideKeyEvent(view, event);
    }
}
}

android清单:(注意:在应用程序标签之前有"android.permission.INTERNET"(

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="pixable.android"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".MyActivity"
            android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

main.xml(我认为它不重要,但我添加它只是为了以防万一(

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <RelativeLayout
        android:id="@+id/index_main_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF" >
        <RelativeLayout
            android:id="@+id/index_header"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:background="#000000" >
            ... My header butons ...
        </RelativeLayout>
        <WebView
            android:id="@+id/index_main_web_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/index_header"
            android:layout_above="@+id/index_botom_layout" />
        <LinearLayout
            android:id="@+id/index_botom_layout"
            android:layout_width="fill_parent"
            android:layout_height="50px"
            android:layout_alignParentBottom="true"
            android:background="#000000"
            android:orientation="horizontal" >
            ... My Footer Butons ...
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

我认为您的问题是您没有在URL前准备http://。我打赌你http://www.google.com有效。

最新更新