当我们像这样在片段中加载Webview时:
private WebView viewer = null;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
viewer = (WebView) inflater
.inflate(R.layout.tut_view, container, false);
return viewer;
}
public void updateUrl(String newUrl) {
if (viewer != null) {
viewer.loadUrl(newUrl);
}
}
但我想使用线性布局而不是网络视图。我尝试这个,但错误出现了。
private LinearLayout viewer ;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
viewer = (LinearLayout) inflater.inflate(R.layout.tut_view, container, false);
View fragment = inflater.inflate(R.layout.tut_view, null);
return viewer;
}
public void updateUrl(String newUrl) {
if (viewer != null) {
}
}
你应该在xml文件中将LinearLayout作为根目录。此外,您不需要投射膨胀的视图,因为返回类型是视图。请阅读片段规格。