У меня проблема с 2 разными WebViews в 2 разных фрагментах.Когда я компилирую приложение с закомментированной частью кода и пытаюсь открыть его на своем телефоне, оно снова закрывается напрямую.Android Studio Doesent дает мне любые ошибки, поэтому я понятия не имею, почему он не работает
Код фрагмента один
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;
}
}
Iобновил код второго фрагмента до этого, потому что в первой версии я допустил небольшую ошибку (я использовал неправильный файл макета):
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;
}
}
И теперь мой телефон не запускает приложение.Я посмотрел в 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>