Opening new android activity with a webview shows a blank (white) screen instead of the html content

前端 未结 11 1440
野性不改
野性不改 2020-12-29 03:29

I have been researching for 2 days and I haven\'t found a similar problem anywhere on the internet!

From the main activity, I\'m trying to open a new activity contai

相关标签:
11条回答
  • 2020-12-29 04:02

    Don't try to load the given url in shouldOverrideUrlLoading, and don't return true from it in your case either. Really, from what it looks like you're trying to do, you shouldn't need to override it at all.

    Returning true from shouldOverrideUrlLoading indicates to the WebView that it should not load the specified URL. What you're doing - telling the WebView to begin loading it again - is likely to cause some sort of internal loop inside the WebView, which I'd guess is why you're seeing odd behavior.

    0 讨论(0)
  • 2020-12-29 04:05

    I can give the another way to solve this problem. Just copy the asset file into the data path "/data/data/org.test.test/" and load it to WebView in the following way.

    String strHTMLFilePath = "file:///data/data/org.test.test/Files/help_android.html";
    webView.loadUrl(strHTMLFilePath);
    

    Use the following code to copy the HTML file from asset to data path

    void getAssetFiles() {
    
        String  WriteFileName = "/data/data/org.test.test/";
            try {
                String list[] = thisActivity.getAssets().list("Files");
                String outFileName = WriteFileName+"Files/";
                if (list != null)
                    for (int i=0; i<list.length; ++i){                          
                    if(!new File(outFileName).exists()){
                        new File(outFileName).mkdirs();
                    }
    
                    OutputStream myOutput = new FileOutputStream(outFileName+list[i]);
    
                    byte[] buffer = new byte[1024];
                    int length;
    
                    String strWritePath = "Files" + "/" + list[i];
    
                    InputStream myInput = thisActivity.getAssets().open(strWritePath);
    
                    while ((length = myInput.read(buffer))>0){
                        myOutput.write(buffer, 0, length);
                    }
                    myInput.close();
    
                    myOutput.flush();
                    myOutput.close();                           
                   }
            } catch (IOException e) {
    
            }
    
        }
    
    0 讨论(0)
  • 2020-12-29 04:12

    May be it was due to hardware acceleration

    webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    

    worked for me.

    https://developer.android.com/guide/topics/graphics/hardware-accel.html

    0 讨论(0)
  • 2020-12-29 04:14

    Try this change in your text.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res/com.example.test"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".text" > <!-- Here is the change -->
    <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:background="@drawable/green"
    >
    <TextView   
    android:id="@+id/apppagetitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#ffffff"
    android:text="Odd News"
    android:textSize="25px"
    android:padding="10px"
    /> 
    </LinearLayout>
    <LinearLayout
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent">
    <WebView  
    android:id="@+id/webkit"
    android:layout_width="fill_parent" 
    android:layout_height="0px"
    android:layout_weight="1"
    /> 
    </LinearLayout>
    </LinearLayout>
    
    0 讨论(0)
  • 2020-12-29 04:15

    Just delete your virtual device and create a new one or test on your own Phone.

    0 讨论(0)
  • 2020-12-29 04:18

    You need to return true in shouldOverrideUrlLoading method

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
    
    0 讨论(0)
提交回复
热议问题