Android Webview教程

时间:2015-08-18 21:53:29

标签: android webview sdk

我正在遵循本指南: https://developer.chrome.com/multidevice/webview/gettingstarted

要制作一个简单的应用程序,使用webview显示我的网站的视图,事实是我下载了android studio并且指南不是最新的(缺少文件fragment_main.xml),所以我绝对无能为力我需要做些什么来启动和运行我的webview,任何帮助?

修改了onCreate方法,它无法解析符号webView,webSettings和方法setJavaScriptEnabled

MainActivity.java

MATCH (u:User)
RETURN u.name, 
       size((u)-[:FOLLOWS]->()) as following, 
       size((u)<-[:FOLLOWS]-()) as followers;

和我的activity_main.xml

package com.example.example.example;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {
private WebView mWebView;

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    mWebView.loadUrl("http://beta.html5test.com/");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

2 个答案:

答案 0 :(得分:3)

更新2

你必须导入相对类“WebSettings” 将此行添加到导入:import android.webkit.WebSettings;

这是为我工作的MainActivity.java

package com.example.example.example;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {
    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl("http://beta.html5test.com/");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

请记住向“清单”添加“互联网权限”,如下所示:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

更新1

您已使用名称“mWebView”声明变量,但在方法中使用“webView”以解决此问题。

我认为此代码应该有效:

private WebView mWebView;

@Override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mWebView = (WebView) findViewById(R.id.activity_main_webview);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.loadUrl("http://beta.html5test.com/");
}

<强>之前:

您是否声明了变量?

与onCreate方法之前的private WebView webView;类似

并且您将变量“webView”与具有国会大厦字母的“WebView”类不匹配。

更改此行WebSettings webSettings = WebView.getSettings();

使用此行:WebSettings webSettings = webView.getSettings();

答案 1 :(得分:0)

Webview类

WebView webView;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_advertising);
    webView = findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setPluginState(WebSettings.PluginState.ON);
    webView.setWebViewClient(new HelloWebViewClient());
    webView.loadUrl("https://www.google.com/");
}

private class HelloWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        return false;
    }
}

网络视图布局

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

<WebView
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center" />
</LinearLayout>

清单权限

<uses-permission android:name="android.permission.INTERNET" />