0

I have a WebView fragment. The problem is that the app crash for some reason instantly.

I read some tutorials and its keep crashing.

I already read this: webview inside fragment android opening outside of the app in a browser and few other topics and nothing worked.

public static class WebFragment extends Fragment {
    public static final String ARG_PLANET_NUMBER = "planet_number";


    public WebFragment() {
            // Empty constructor required for fragment subclasses
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) 
    {     
        View rootView = inflater.inflate(R.layout.fragment_web, container, false);
        int i = getArguments().getInt(ARG_PLANET_NUMBER);
        String planet = getResources().getStringArray(R.array.planets_array)[i];

        WebView webview;
        webview = (WebView) getActivity().findViewById(R.id.webview);
        webview.setWebViewClient(new WebViewClient());
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setUseWideViewPort(true);
        webview.setWebChromeClient(new WebChromeClient());
        webview.loadUrl("http://www.klh-dev.com/lehava/lehava/system/mlogin.php");

        getActivity().setTitle(planet);

        return rootView;
    }
}`

logcat:

10-31 09:59:00.127: E/AudioManagerAndroid(24850): BLUETOOTH permission is missing!
10-31 09:59:00.187: W/chromium(24850): [WARNING:proxy_service.cc(890)] PAC support disabled because there is no system implementation
10-31 09:59:00.217: D/AndroidRuntime(24850): Shutting down VM
10-31 09:59:00.217: W/dalvikvm(24850): threadid=1: thread exiting with uncaught exception (group=0x41650ce0)
10-31 09:59:00.217: E/AndroidRuntime(24850): FATAL EXCEPTION: main
10-31 09:59:00.217: E/AndroidRuntime(24850): Process: com.example.android.navigationdrawerexample, PID: 24850
10-31 09:59:00.217: E/AndroidRuntime(24850): java.lang.NullPointerException
10-31 09:59:00.217: E/AndroidRuntime(24850):    at com.example.android.navigationdrawerexample.MainActivity$WebFragment.onCreateView(MainActivity.java:328)
10-31 09:59:00.217: E/AndroidRuntime(24850):    at android.app.Fragment.performCreateView(Fragment.java:1700)
10-31 09:59:00.217: E/AndroidRuntime(24850):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
10-31 09:59:00.217: E/AndroidRuntime(24850):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
10-31 09:59:00.217: E/AndroidRuntime(24850):    at android.app.BackStackRecord.run(BackStackRecord.java:684)
10-31 09:59:00.217: E/AndroidRuntime(24850):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
10-31 09:59:00.217: E/AndroidRuntime(24850):    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
10-31 09:59:00.217: E/AndroidRuntime(24850):    at android.os.Handler.handleCallback(Handler.java:733)
10-31 09:59:00.217: E/AndroidRuntime(24850):    at android.os.Handler.dispatchMessage(Handler.java:95)
10-31 09:59:00.217: E/AndroidRuntime(24850):    at android.os.Looper.loop(Looper.java:136)
10-31 09:59:00.217: E/AndroidRuntime(24850):    at android.app.ActivityThread.main(ActivityThread.java:5144)
10-31 09:59:00.217: E/AndroidRuntime(24850):    at java.lang.reflect.Method.invokeNative(Native Method)
10-31 09:59:00.217: E/AndroidRuntime(24850):    at java.lang.reflect.Method.invoke(Method.java:515)
10-31 09:59:00.217: E/AndroidRuntime(24850):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
10-31 09:59:00.217: E/AndroidRuntime(24850):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
10-31 09:59:00.217: E/AndroidRuntime(24850):    at dalvik.system.NativeStart.main(Native Method)

**Sorry my English isn't the best.

Thank you, Morha13

Community
  • 1
  • 1
user3825694
  • 59
  • 1
  • 9

1 Answers1

1

It should be webview = (WebView) rootView.findViewById(R.id.webview);.

The WebView is in your newly inflated fragment, not in the activity, not yet at least.

Eduard B.
  • 6,735
  • 4
  • 27
  • 38