简体   繁体   English

片段中的Android WebView

[英]Android WebView in Fragment

I know the following is probably simple, but I can't seem to get it to work. 我知道以下内容可能很简单,但是我似乎无法使其正常工作。 It compile and runs, but doesn't load any URL I throw at it. 它可以编译并运行,但不会加载我抛出的任何URL。 I have Internet set in permisions, even checked to make sure it was running in some other code. 我在Internet中设置了权限,甚至检查过Internet以确保它以其他代码运行。

package com.richardmather.autoaccidentattorney;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;

public class FindHospitalFragment extends Fragment {

    public FindHospitalFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.fragment_find_hospital, container, false);
        final WebView webView = (WebView) rootView.findViewById(R.id.webView);
        String searchURL = "http://www.stackoverflow.com";
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("searchURL");

        return rootView;
    }
}

Here is the XML 这是XML

<?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">


    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</LinearLayout>

I know it's something simple I'm missing. 我知道这很简单,我很想念。

EDIT: Took out the quotation marks, now it's loading the URL in Google Chrome.... 编辑:拿出引号,现在它正在Google Chrome中加载URL。

The problem probably is the line webView.loadURL("searchURL"); 问题可能是webView.loadURL("searchURL"); .

By doing that it actually tries to load the URL searchURL and not the URL http://www.stackoverflow.com you saved in the variable searchURL . 这样,它实际上会尝试加载URL searchURL而不是加载您保存在变量searchURL的URL http://www.stackoverflow.com

So just remove the " s: 因此,只需删除"

webView.loadURL(searchURL); // searchURL = "http://www.stackoverflow.com"

Edit : 编辑

To not launch chrome, simply add the following line: 要不启动chrome,只需添加以下行:

webview.setWebViewClient(new WebViewClient());

Also see this question for details. 另请参阅问题以获取详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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