1

I'm having this error does anyone know how to fix it? Any help is welcome. thank you. i'm using fragment

class ForumFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {



      ERROR LINE->  val wv = webView.findViewById(R.id.webView) as WebView

        webView.loadUrl("https://www.google.com/");
        webView.clearView();
        webView.measure(100, 100);
        webView.getSettings().setUseWideViewPort(true)
        webView.getSettings().setLoadWithOverviewMode(true);

        return inflater.inflate(R.layout.fragment_forum, container, false)
    }

  }

LOGCAT ERROR ->

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
                at .fragments.ForumFragment.onCreateView(ForumFragment.kt:45
2
  • In onCreateView first you must inflate your layout which returns view, and then using that view instantiate your webview Commented Jun 30, 2019 at 6:06
  • Could you post the layout of the fragment you're using? Where are you accessing the instance of webView from? Commented Jun 30, 2019 at 6:07

1 Answer 1

1
class ForumFragment : Fragment() {

    private lateinit var v: View
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        v = inflater.inflate(R.layout.frag_list_project, container, false)
        val wv = v.findViewById(R.id.webView) as WebView

        wv.loadUrl("https://www.google.com/");
        wv.clearView();
        wv.measure(100, 100);
        wv.getSettings().setUseWideViewPort(true)
        wv.getSettings().setLoadWithOverviewMode(true);
        return v

    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.