我遇到此错误,有人知道如何解决吗? 欢迎任何帮助。谢谢。我正在使用片段
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
答案 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
}