WebView不加载页面

发布于 2025-01-24 10:06:32 字数 6663 浏览 0 评论 0原文

我尝试在fragment1.java中添加WebView。问题是代码运行,但对于每个网站,它都会向我显示一个白屏,没有崩溃或错误消息。在添加WebView之前,它的外观

package com.example.bottomnavigationview;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class Fragment1 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment1,container,false);
        return rootView;
    }

}

是:

package com.example.bottomnavigationview;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

public class Fragment1 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment1,container,false);
        return rootView;
    }

    public class MainActivity extends AppCompatActivity {
        String websiteURL = "https://www.example.com"; // sets web url
        private WebView webview;
        SwipeRefreshLayout mySwipeRefreshLayout;

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

            if( ! CheckNetwork.isInternetAvailable(this)) //returns true if internet available
            {
                //if there is no internet do this
                setContentView(R.layout.activity_main);
                //Toast.makeText(this,"No Internet Connection, Chris",Toast.LENGTH_LONG).show();

                new AlertDialog.Builder(this) //alert the person knowing they are about to close
                        .setTitle("No internet connection available")
                        .setMessage("Please Check you're Mobile data or Wifi network.")
                        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }
                        })
                        //.setNegativeButton("No", null)
                        .show();

            }
            else
            {

                //Webview stuff
                webview = findViewById(R.id.webView);
                webview.getSettings().setJavaScriptEnabled(true);
                webview.getSettings().setDomStorageEnabled(true);
                webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
                webview.loadUrl(websiteURL);
                webview.setWebViewClient(new WebViewClientDemo());

            }

            //Swipe to refresh functionality
            mySwipeRefreshLayout = (SwipeRefreshLayout)this.findViewById(R.id.swipeContainer);

            mySwipeRefreshLayout.setOnRefreshListener(
                    new SwipeRefreshLayout.OnRefreshListener() {
                        @Override
                        public void onRefresh() {
                            webview.reload();
                        }
                    }

            );

        }

        private class WebViewClientDemo extends WebViewClient {
            @Override
            //Keep webview in app when clicking links
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                mySwipeRefreshLayout.setRefreshing(false);

            }

        }

        //set back button functionality
        @Override
        public void onBackPressed() { //if user presses the back button do this
            if (webview.isFocused() && webview.canGoBack()) { //check if in webview and the user can go back
                webview.goBack(); //go back in webview
            } else { //do this if the webview cannot go back any further
                new AlertDialog.Builder(this) //alert the person knowing they are about to close
                        .setTitle("EXIT")
                        .setMessage("Are you sure. You want to close this app?")
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }
                        })
                        .setNegativeButton("No", null)
                        .show();
            }

        }

    }

    class CheckNetwork {

        private static final String TAG = CheckNetwork.class.getSimpleName();

        public static boolean isInternetAvailable(Context context)

        {
            NetworkInfo info = (NetworkInfo) ((ConnectivityManager)

                    context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

            if (info == null)

            {
                Log.d(TAG,"no internet connection");

                return false;

            }
            else
            {

                if(info.isConnected())

                {

                    Log.d(TAG," internet connection available...");

                    return true;

                }

                else

                {

                    Log.d(TAG," internet connection");

                    return true;
                }
            }
        }

}

我认为它需要以其他方式进行。有人可以帮忙吗?因为应用程序中的页面不要下载。************************************************************************************ *************************************

I trying add webview in fragment1.java. The problem is the code runs but for every website it shows me a white screen, no crashing or error messages. This how it looks before add WebView:

package com.example.bottomnavigationview;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class Fragment1 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment1,container,false);
        return rootView;
    }

}

And after:

package com.example.bottomnavigationview;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

public class Fragment1 extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment1,container,false);
        return rootView;
    }

    public class MainActivity extends AppCompatActivity {
        String websiteURL = "https://www.example.com"; // sets web url
        private WebView webview;
        SwipeRefreshLayout mySwipeRefreshLayout;

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

            if( ! CheckNetwork.isInternetAvailable(this)) //returns true if internet available
            {
                //if there is no internet do this
                setContentView(R.layout.activity_main);
                //Toast.makeText(this,"No Internet Connection, Chris",Toast.LENGTH_LONG).show();

                new AlertDialog.Builder(this) //alert the person knowing they are about to close
                        .setTitle("No internet connection available")
                        .setMessage("Please Check you're Mobile data or Wifi network.")
                        .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }
                        })
                        //.setNegativeButton("No", null)
                        .show();

            }
            else
            {

                //Webview stuff
                webview = findViewById(R.id.webView);
                webview.getSettings().setJavaScriptEnabled(true);
                webview.getSettings().setDomStorageEnabled(true);
                webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
                webview.loadUrl(websiteURL);
                webview.setWebViewClient(new WebViewClientDemo());

            }

            //Swipe to refresh functionality
            mySwipeRefreshLayout = (SwipeRefreshLayout)this.findViewById(R.id.swipeContainer);

            mySwipeRefreshLayout.setOnRefreshListener(
                    new SwipeRefreshLayout.OnRefreshListener() {
                        @Override
                        public void onRefresh() {
                            webview.reload();
                        }
                    }

            );

        }

        private class WebViewClientDemo extends WebViewClient {
            @Override
            //Keep webview in app when clicking links
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;

            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                mySwipeRefreshLayout.setRefreshing(false);

            }

        }

        //set back button functionality
        @Override
        public void onBackPressed() { //if user presses the back button do this
            if (webview.isFocused() && webview.canGoBack()) { //check if in webview and the user can go back
                webview.goBack(); //go back in webview
            } else { //do this if the webview cannot go back any further
                new AlertDialog.Builder(this) //alert the person knowing they are about to close
                        .setTitle("EXIT")
                        .setMessage("Are you sure. You want to close this app?")
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                finish();
                            }
                        })
                        .setNegativeButton("No", null)
                        .show();
            }

        }

    }

    class CheckNetwork {

        private static final String TAG = CheckNetwork.class.getSimpleName();

        public static boolean isInternetAvailable(Context context)

        {
            NetworkInfo info = (NetworkInfo) ((ConnectivityManager)

                    context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

            if (info == null)

            {
                Log.d(TAG,"no internet connection");

                return false;

            }
            else
            {

                if(info.isConnected())

                {

                    Log.d(TAG," internet connection available...");

                    return true;

                }

                else

                {

                    Log.d(TAG," internet connection");

                    return true;
                }
            }
        }

}

I think it needs to be put in some other way. Can somebody help? Because page in app don't downloading.***************************************************************************

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

垂暮老矣 2025-01-31 10:06:32

遵循此代码段。

  WebSettings webSettings = webView.getSettings();
  webSettings.setJavaScriptEnabled(true);
  webView.addJavascriptInterface(new WebAppInterface(), "Android");
  webView.setWebViewClient(new WebViewClient());
  webView.loadDataWithBaseURL(null, YOUR_URL, "text/html", "utf-8", null);

在这里,WebView将被您的视图对象替换。

注意:AddJavaScript Interface是可选的。它将在您需要Web Cromeclient回调的情况下使用它。另外,有关AddJavascriptInterface的更多详细信息,您可以参考

follow this code snippet.

  WebSettings webSettings = webView.getSettings();
  webSettings.setJavaScriptEnabled(true);
  webView.addJavascriptInterface(new WebAppInterface(), "Android");
  webView.setWebViewClient(new WebViewClient());
  webView.loadDataWithBaseURL(null, YOUR_URL, "text/html", "utf-8", null);

here, webView will be replaced with your view object.

Note: addJavascriptInterface is optional. It will be used in the case when you want a callback from webChromeClient. also, for more details about addJavascriptInterface, you can refer to https://stackoverflow.com/a/11572941/8237887

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文