繁体   English   中英

Android WebView显示黑屏

[英]Android WebView shows black screen

我正在为Android WebView尝试以下示例:

http://www.linuxtopia.org/online_books/android/devguide/guide/tutorials/views/hello-webview.html

但我没有得到想要的东西,这是我的文件:

MainActivity.java:

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

   private WebView webview;

   /**
    * Called when the activity is first created.
    */
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      webview = (WebView) findViewById(R.id.webview);
      webview.setWebViewClient(new WebClient());
      webview.getSettings().setJavaScriptEnabled(true);
      webview.loadUrl("http://www.google.com");
   }

   private class WebClient extends WebViewClient {

      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
         view.loadUrl(url);
         return true;

      }
   }
}

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <WebView 
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />

</LinearLayout>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.sheya"
          android:versionCode="1"
          android:versionName="1.0">
   <uses-permission android:name="android.permission.INTERNET" />
   <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
      <activity android:name="MainActivity"
                  android:label="@string/app_name">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

事实是,当我启动应用程序时,它显示黑屏,但是如果我删除了

webview.setWebViewClient(new WebClient());

从应用程序代码行开始,它将Google页面打开到默认浏览器...

-编辑-

main.xml布局文件中解决了该问题:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" <!-- Instead of wrap_content -->
    android:layout_height="fill_parent" <!-- Instead of wrap_content -->
    android:orientation="vertical">

    <WebView 
        android:id="@+id/webview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    />

</LinearLayout>

如果你尝试这个怎么样

@Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
      return super.shouldOverrideUrlLoading(view, url);
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM