简体   繁体   中英

WebView displays black screen

I'm a little embarrassed to post this but I can't seem to figure out where I'm going wrong. I've looked at every example and every tutorial and everything looks right to me. Here's what I'm doing. I have a listview that when you click on an item it will take you to a WebView that displays some static formatted text associated with that list entry.

I had it all working with a TextView but I wanted to be able to use HTML formatting for the text and figured the WebView was the way to go. Right now it is just supposed to display a generic link for testing purposes but when the viewContent intent starts it just goes to a black screen. I can go back and pick another entry and it also just shows the black screen.

I'm not sure what code you are going to want to see so here's the viewSection class file and the viewsection.xml layout.

viewSection.java:

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

public class viewSection extends Activity {

       /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       WebView wv;

       setContentView(R.layout.viewsection);

       wv = (WebView) findViewById(R.id.wv1);
       wv.loadData("<a href='x'>Hello World! - 1</a>",
                                               "text/html",
                                               "utf-8");
   }
}

viewsection.xml:

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

You probably want to set android:layout_height on the WebView to fill_parent . I'm not sure if WebView supports wrap_content .

EDIT : You'll want to set the LinearLayout width and height to fill_parent as well.

Also, if you're using very light HTML styling, you can still use a TextView; there are samples in the API Demos sample app on how to do this (ie StyledText.java and Link.java ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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