简体   繁体   中英

Android webview not loading url?

I have a webview set up but when I click on the item, an empty fragment shows up where the actual web item is supposed to be. I have tried loading my urls on mobile browser and they work fine. I have an internet permission in android manifest.

web view layout file

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.HomeActivity">

    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

webview fragment

public class WebViewDetailsFragment extends Fragment {

    private WebView detailsWebView;
    private String articleSource;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_web_view_details, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        detailsWebView = view.findViewById(R.id.webView);
        detailsWebView.getSettings().setLoadsImagesAutomatically(true);
        detailsWebView.getSettings().setJavaScriptEnabled(true);
        detailsWebView.getSettings().setUseWideViewPort(true);
        detailsWebView.getSettings().setLoadWithOverviewMode(true);
        detailsWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        // Configure the client to use when opening URLs
        detailsWebView.setWebViewClient(new WebViewClient());
        setItems();
        detailsWebView.loadUrl(articleSource);


    }

    private void setItems(){
        NewsArticle newsArticle = NewsFragment.news.get(NewsAdapter.pos);
        articleSource = "https://www.google.com";
    }
}

I too tried your Code. it is working fine and good.There may be a chances of issue in fragmentManager. Did you checked the view is visible or not

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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