【问题标题】:Android Content view not yet created fragmentAndroid 内容视图尚未创建片段
【发布时间】:2016-09-07 10:50:12
【问题描述】:

目前我有一个包含 4 个片段的选项卡布局设置。我有一个片段,最初我在片段之间切换时出现重复的 id 错误。我设法通过应用下面的代码解决了这个问题,但是现在我在切换时收到一个内容视图尚未创建的消息。

public class contactus2 extends Fragment {

private static View view;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    if (view != null) {
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null)
            parent.removeView(view);
    }
    try {
        view = inflater.inflate(R.layout.contactus2, container, false);
    } catch (InflateException e) {
        Log.i("Inflate", "View Hidden");

    }
    return view;
}

}

【问题讨论】:

  • 能否请您提供实际的android错误。

标签: android android-fragments error-handling compiler-errors fragment


【解决方案1】:

onCreateView() 永远不应引用静态视图对象。循环浏览应用程序时,Android 将拆除并重建片段的视图。因此,您应该始终在 onCreateView 中填充新视图。按照您现在的方式,您的应用将尝试为多个片段重用一个视图。

类似这样的:

public class contactus2 extends Fragment {

    private View rootView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.contactus2, container, false);
        return rootView;
    }
}

基本上,移除对片段视图引用的静态分配。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-21
    • 2014-04-13
    • 1970-01-01
    • 2014-10-04
    • 1970-01-01
    • 1970-01-01
    • 2014-12-08
    • 1970-01-01
    相关资源
    最近更新 更多