机器人片段被赋予一个空容器中onCreateView() [英] Android Fragment is given a null container in onCreateView()

查看:134
本文介绍了机器人片段被赋予一个空容器中onCreateView()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Android的碎片在一个非常简单的方法,类似于在Android开发者网站上的教程。

I am trying to use Android fragments in a very simple way, similar to the tutorial on the Android developer website.

我有一个活动(MediaInfoActivity)具有以下code:

I have an Activity (MediaInfoActivity) with the following code:

public class MediaInfoActivity extends FragmentActivity {
    private final String TAG = "MediaInfoActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate()");
        setContentView(R.layout.media_info_activity_layout);
    }

}

下面是code为media_info_activity_layout.xml文件:

Here is the code for the media_info_activity_layout.xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" 
    android:layout_height="match_parent"
    android:orientation="vertical">

    <fragment class="com.hawkforce.test.MediaInfoFragment"
            android:id="@+id/mediaInfoFragment"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="0dp" />

    <FrameLayout android:id="@+id/mediaPlayerBarPanel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

    <fragment class="com.hawkforce.test.MediaPlayerBarFragment"
                android:id="@+id/mediaPlayerBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

    </FrameLayout>

和终于在这里是$ C $下MediaInfoFragment:

And finally here is the code for MediaInfoFragment:

public class MediaInfoFragment extends Fragment {
    private final static String TAG = "MediaInfoFragment";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(TAG, "onCreate()");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        Log.i(TAG, "onCreateView()");
        if (container == null) {
            Log.i(TAG, "onCreateView(): container = null");
        }

        return inflater.inflate(R.layout.media_info_fragment_layout, container, false);
    }

}

下面是我的问题:在MediaInfoFragment的onCreateView()方法传递的容器是空。我的理解,这应该只是针对非UI片段的情况。然而,我的片段有一个用户界面,在显示的时候我推出MediaInfoActivity在屏幕上确定。它会导致问题,因为在片段的XML布局文件中声明没有的风格。

Here is my problem : the container passed in the onCreateView() method of the MediaInfoFragment is null. As I understood, this should only be the case for non-UI Fragments. However, my Fragment has a UI, which is displayed OK on the screen when I launch MediaInfoActivity. It causes problems because no style declared in the xml layout file of the fragment is applied.

下面是我的记录:

I/MediaInfoActivity: onCreate()
I/MediaInfoFragment: onCreate()
I/MediaInfoFragment: onCreateView()
I/MediaInfoFragment: onCreateView(): container = null

我缺少什么明显的位置?

Am I missing anything obvious here ?

推荐答案

您只需要创建像跟随你片段充气。

You just have to create a inflater like follow in your fragment.

View rootView;
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    if (rootView == null) {
        rootView = inflater.inflate(R.layout.activity_my_cart, null);
    } else {
        ((ViewGroup) container.getParent()).removeView(rootView);
    }
    return rootView;
}

我希望它将按照你的问题。

I hope it will work as per your question.

这篇关于机器人片段被赋予一个空容器中onCreateView()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆