簡體   English   中英

Android WebView無法加載URL?

[英]Android webview not loading url?

我已經設置了一個Web視圖,但是當我單擊該項目時,將顯示一個空白片段,它應該位於實際的Web項目應該位於的位置。 我試過在移動瀏覽器上加載我的網址,它們工作正常。 我在Android清單中具有互聯網許可。

Web視圖布局文件

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

網絡視圖片段

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

我也嘗試過您的代碼。 它工作正常且良好。fragmentManager中可能存在問題。 您是否檢查過視圖是否可見

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM