kotlin webview android.view.View android.view.View.findViewById(int)'上的空对象引用ko

时间:2019-06-30 05:54:31

标签: android kotlin android-webview

我遇到此错误,有人知道如何解决吗? 欢迎任何帮助。谢谢。我正在使用片段

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错误->

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

1 个答案:

答案 0 :(得分:2)

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

    }