两个片段中的两个 Webview 的问题

Problems with two Webviews in two Fragments

我对 2 个不同片段中的 2 个不同 WebView 有疑问。当我用现在注释掉的代码部分编译应用程序,并尝试在我的 phone 上打开它时,它又直接关闭了。 Android Studio 没有给我任何错误,所以我不知道为什么它不起作用

片段一代码

package de.thomaeum.thomapp10;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

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

public class Frag_Unterstufe extends Fragment {

    public WebView webViewUnterstufe;
    public String pdfUnterstufe = "http://drive.google.com/viewerng/viewer?embedded=true&url=" + "https://www.thomaeum.de/download/thom_si.pdf";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.frag_unterstufe_layout, container, false);
        webViewUnterstufe = (WebView) v.findViewById(R.id.oberstufe);
        webViewUnterstufe.loadUrl(pdfUnterstufe);

        // Enable Javascript
        WebSettings webSettings = webViewUnterstufe.getSettings();
        webSettings.setJavaScriptEnabled(true);

        // Force links and redirects to open in the WebView instead of in a browser
        webViewUnterstufe.setWebViewClient(new WebViewClient());

        return v;
    }
}

片段二代码

package de.thomaeum.thomapp10;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

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

public class Frag_Oberstufe extends Fragment {

    public WebView webViewOberstufe;
    public String pdfOberstufe = "https://www.thomaeum.de/download/thom2.pdf";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View vO=inflater.inflate(R.layout.frag_unterstufe_layout, container, false);
        /*
        webViewOberstufe = (WebView) vO.findViewById(R.id.oberstufe);
        webViewOberstufe.loadUrl(pdfOberstufe);

        // Enable Javascript
        WebSettings webSettingsO = webViewOberstufe.getSettings();
        webSettingsO.setJavaScriptEnabled(true);

        // Force links and redirects to open in the WebView instead of in a browser
        webViewOberstufe.setWebViewClient(new WebViewClient());
        */
        return vO;
    }
}

我将第二个片段的代码更新为这个,因为在第一个版本中我犯了一个小错误(我使用了错误的布局文件):

package de.thomaeum.thomapp10;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

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

public class Frag_Oberstufe extends Fragment {

    public WebView webViewOberstufe;
    public String pdfOberstufe = "https://www.thomaeum.de/download/thom2.pdf";

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View vO=inflater.inflate(R.layout.frag_oberstufe_layout, container, false);

        webViewOberstufe = (WebView) vO.findViewById(R.id.oberstufe);
        webViewOberstufe.loadUrl(pdfOberstufe);

        // Enable Javascript
        WebSettings webSettingsO = webViewOberstufe.getSettings();
        webSettingsO.setJavaScriptEnabled(true);

        // Force links and redirects to open in the WebView instead of in a browser
        webViewOberstufe.setWebViewClient(new WebViewClient());

        return vO;
    }
}

现在我的 Phone 没有启动应用程序。 我调查了 Logcat,它给了我这个错误:

2019-09-24 09:02:06.274 27925-27925/de.thomaeum.thomapp10 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: de.thomaeum.thomapp10, PID: 27925
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.loadUrl(java.lang.String)' on a null object reference
        at de.thomaeum.thomapp10.Frag_Oberstufe.onCreateView(Frag_Oberstufe.java:26)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2439)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
        at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
        at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:802)
        at androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
        at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
        at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
        at androidx.fragment.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2243)
        at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:654)
        at androidx.fragment.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:146)
        at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1244)
        at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1092)
        at androidx.viewpager.widget.ViewPager.onMeasure(ViewPager.java:1622)
        at android.view.View.measure(View.java:23466)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6834)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1565)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:726)
        at android.view.View.measure(View.java:23466)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6834)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
        at android.view.View.measure(View.java:23466)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6834)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1565)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:726)
        at android.view.View.measure(View.java:23466)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6834)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:23466)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6834)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1565)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:726)
        at android.view.View.measure(View.java:23466)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6834)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:847)
        at android.view.View.measure(View.java:23466)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2954)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1753)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2041)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1636)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7946)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1092)
        at android.view.Choreographer.doCallbacks(Choreographer.java:893)
        at android.view.Choreographer.doFrame(Choreographer.java:812)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1078)
        at android.os.Handler.handleCallback(Handler.java:907)
        at android.os.Handler.dispatchMessage(Handler.java:105)
2019-09-24 09:02:06.275 27925-27925/de.thomaeum.thomapp10 E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7625)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)

XML 代码 Frag_Unterstufe

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_dark">

    <WebView
        android:id="@+id/unterstufe"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

XML 代码 Frag_Oberstufe

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/background_light">

    <WebView
        android:id="@+id/oberstufe"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


</androidx.constraintlayout.widget.ConstraintLayout>

清理项目->重建项目。有时会发生。