android - 作为 fragment 子对象的空对象

标签 android android-layout android-fragments android-viewpager viewgroup

我有一个 Fragment,它是 ViewPager 的一部分。在这个 Fragment 中,我有一个 ViewGroup child 。现在,为什么在实例化了我的 ViewPageradapter 之后,在我的 MainActivityonCreate() 中,我的 Container 正在获取 null

这是我的onCreate():

private MyAdapter mAdapter;
private ViewPager mPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mAdapter = new MyAdapter(getSupportFragmentManager());

    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);

    mContainerView = (ViewGroup) findViewById(R.id.container);
    //Here mContainerView is already null
    ...
}

这是 Fragment,它是包含 mContainerViewViewPager 的一部分>

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<ScrollView android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!-- This is my ViewGroup -->
   <LinearLayout android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:showDividers="middle"
        android:divider="?android:dividerHorizontal"
        android:animateLayoutChanges="true"
        android:paddingLeft="16dp"
        android:paddingRight="16dp" />

</ScrollView>

<TextView android:id="@android:id/empty"
    style="?android:textAppearanceSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:padding="32dp"
    android:text="@string/message_empty_layout_changes"
    android:textColor="?android:textColorSecondary" />

</FrameLayout>

最佳答案

如果我没看错你的问题,你正在尝试访问 FragmentView (和 child )使用 ActivityfindViewById()方法。自 Fragment 以来,这不应该起作用膨胀自己的布局和Fragments 不是传统Views .

如果您知道 Fragment已实例化并且您可以检索它,您可以获得 ViewGroup 的实例使用

yourFragment#getView().findViewById()

如果没有,你可以制作一个你的界面 Activity使用接受 ViewGroup 的方法实现作为论据。然后在 fragment 的onCreateView() , 让 Fragment 通过 ViewGroup关闭到界面。您可以直接转换到您的 Activity , 但界面更简洁。

例如

public class Fragment {

 public interface ViewGroupCreateListener{

     public void onViewGroupCreated (ViewGroup v);
 }

 private ViewGroupCreateListener listener;

 public void onAttach (Activity a){
     super.onAttach (a);
     listener = (ViewGroupCreateListener) a;
 }

public View onCreateView (/*all its arguments here*/){
    View v = inflater.inflate (R.layout.your_layout);
    ViewGroup group = v.findViewById (R.id.container);
    listener.onViewGroupCreated(group);

    return v;
 }

你的 Activity看起来像:

public class MainActivity extends Activity implements ViewGroupCreateListener, OtherInterface1, OtherInterface2{

  private ViewGroup mViewGroup;


  public void onViewGroupCreated (ViewGroup v){
     mViewGroup = v;
  }
}

这很好,因为如果寻呼机重新实例化 Fragment , Activity 仍然获得 ViewGroup 的有效实例.

或者,如果这取决于您实际尝试使用此 ViewGroup 实现的目标,您可以在 Fragment 中执行此处理本身。

关于android - 作为 fragment 子对象的空对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18092806/

相关文章:

android 服务器消息未显示在聊天屏幕上

android - 如何在android中获取当前的wifi连接信息

Android - 如何覆盖 "Back"按钮,使其不会完成()我的 Activity ?

android - 使 MyView 可见会导致 RelativeLayout 向下移动

android - 自定义布局上的 Onlayout 方法扩展 LinearLayout

java - android 如何动态设置样式/如何读取样式属性

android - 启用语言后 CMake 错误 : CMAKE_C_COMPILER not set,

android - Activity.isFinishing() 的 fragment 等价物是什么?

java - Context.getContentResolver().query() 给出空指针异常

android - ListView 项目中的 YouTubePlayerSupportFragment 为空