0

当我们像这样在片段中加载 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);
        }
    }

但我想使用线性布局而不是 webview。我试试这个,但错误出来了。

 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) {


        }
    }
4

1 回答 1

1

您应该在 xml 文件中将 LinearLayout 作为根。此外,您不需要将膨胀视图转换为返回类型为视图。请阅读片段规范

于 2012-11-30T05:48:37.227 回答