Hello, if you click the Back button in WebView within Android Fragment... (This is a beginner.)

Asked 2 years ago, Updated 2 years ago, 201 views

            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (event.getAction() != KeyEvent.ACTION_DOWN)
                    return true;


                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    if (mWebView.canGoBack()) {
                        mWebView.goBack();
                    } } else


                    return true;
                }
        });

I have applied web view to Fragment If you click the "Go Back" button on your phone, you don't go out of the activity I wrote the code in the Fragment so that the back page can be applied in the web view. When you run the app, simply go to this fragment and the app stops. Is there any part of the code that needs to be fixed? Thank you for your help.

android fragment

2022-09-22 21:32

2 Answers

NullPointerException occurs in <init> in LostFragment, so the mWebView.setOnKeyListener() function appears to have been invoked from the constructor in the Fragment. The mWebView has a null state because the View is not bound at the time the constructor of the fragment is called. Write the setOnKeyListener code in the onCreateView() function.

Please refer to the Lifecycle link in Fragment for the reasons for doing this.

https://developer.android.com/guide/components/fragments.html?hl=ko


2022-09-22 21:32

05-19 19:52:45.207 5033-5033/com.chani.ssari E/AndroidRuntime: FATAL EXCEPTION: main
                                                               Process: com.chani.ssari, PID: 5033
                                                               java.lang.NullPointerException: Attempt to invoke virtual method 'void android.webkit.WebView.setOnKeyListener(android.view.View$OnKeyListener)' on a null object reference
                                                                   at com.chani.ssari.LostFragment.<init>(LostFragment.java:205)
                                                                   at com.chani.ssari.TabFragment$MyAdapter.getItem(TabFragment.java:73)
                                                                   at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:97)
                                                                   at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:943)
                                                                   at android.support.v4.view.ViewPager.populate(ViewPager.java:1091)
                                                                   at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:608)
                                                                   at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:570)
                                                                   at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:551)
                                                                   at android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:2008)
                                                                   at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1025)
                                                                   at android.support.design.widget.TabLayout.selectTab(TabLayout.java:995)
                                                                   at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1272)
                                                                   at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:1377)
                                                                   at android.view.View$PerformClick.run(View.java:22526)
                                                                   at android.os.Handler.handleCallback(Handler.java:739)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                   at android.os.Looper.loop(Looper.java:158)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:7224)
                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

The error log appears like this...


2022-09-22 21:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.