为什么我的WebView无法正常工作,呈现错误?

时间:2014-05-20 13:16:23

标签: android

我几乎放弃了。我不知道我的错误是什么,我的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>

3 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

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

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

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

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

答案 2 :(得分:0)

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);
    }
}