gpt4 book ai didi

Android Fragment 在 onCreateView() 中被赋予一个空容器

转载 作者:可可西里 更新时间:2023-11-01 19:07:32 39 4
gpt4 key购买 nike

我正在尝试以一种非常简单的方式使用 Android fragment ,类似于 Android 开发者网站上的教程。

我有一个具有以下代码的 Activity (MediaInfoActivity):

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);
}

}

这是 media_info_activity_layout.xml 文件的代码:

<?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>

最后是 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 fragment 。但是,我的 Fragment 有一个 UI,当我启动 MediaInfoActivity 时,它在屏幕上显示正常。它会导致问题,因为没有应用 fragment 的 xml 布局文件中声明的样式。

这是我的日志:

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

我在这里遗漏了什么明显的东西吗?

最佳答案

你只需要在你的 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;
}

我希望它能按照您的问题工作。

关于Android Fragment 在 onCreateView() 中被赋予一个空容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16883831/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com