【问题标题】:Why my WebView doesn't work properly, wrong rendering?为什么我的 WebView 不能正常工作,渲染错误?
【发布时间】:2014-05-20 13:16:22
【问题描述】:

我几乎放弃了。我不知道我做错了什么,我的 HTML 代码没有呈现,我得到了webpage not found file:///.... 的错误。我尝试根据Problems loading html asset into webview + 这是我的代码:

主java类MainActivity.java

package com.example.webviewvulnerability;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

private Button button;




public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    final String URL="file:///home/m.sepczuk/Downloads/eclipse/workspace/WebViewVulnerability/assets/index.html";
    final WebView webview = new WebView(this);
    webview.getSettings().setJavaScriptEnabled(true);
    webview.setWebViewClient(new WebViewClient());
    webview.loadUrl(URL);

}

清单文件Manifest file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.webviewvulnerability"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.webviewvulnerability.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>

/assets 目录下的索引文件/assets/index.html

index
<!doctype html>
<html>
<head>
</head>
<body>
    <center>
        <div id="msg">It works!!!</div>
    </center>
</body>
</html>

【问题讨论】:

    标签: android


    【解决方案1】:

    您为 webview 提供了错误的 url。网址应该是这样的:

    wv.loadUrl("file:///android_asset/index.html");
    

    【讨论】:

      【解决方案2】:

      首先,你的 Webview 没有在任何地方使用。在你的 main.xml 中创建一个 Webview,然后通过调用来初始化它:

        Webview webview = (Webview) findViewById(R.id.yourwebviewid); 
      

      我认为你的网址不正确,你应该使用类似的东西:

        webview.loadUrl("file:///android_asset/index.html");
      

      【讨论】:

      • 不是“file:///android_res/raw/...”吗?
      【解决方案3】:
      package com.example.webviewvulnerability;
      
      import android.annotation.SuppressLint;
      import android.app.Activity;
      import android.os.Bundle;
      import android.webkit.WebView;
      import android.webkit.WebViewClient;
      import android.widget.Button;
      import android.widget.LinearLayout;
      
      public class MainActivity extends Activity {
      
          private Button button;
      
          @SuppressLint("SetJavaScriptEnabled")
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              //URL for html file in assets should look like this
              final String URL = "file:///android_asset/index.html";
              final WebView webview = new WebView(this);
              webview.getSettings().setJavaScriptEnabled(true);
              webview.setWebViewClient(new WebViewClient());
              webview.loadUrl(URL);
      
              //Reference to your layout and then add webview to it
              LinearLayout llMain=(LinearLayout) findViewById(R.id.llMain);
              llMain.addView(webview);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-06
        相关资源
        最近更新 更多