空的对象是一个片段的子

空的对象是一个片段的子

本文介绍了空的对象是一个片段的子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段这是一个 ViewPager 部分。在这种片段我有一个的ViewGroup 的孩子。现在,为什么在我的 MainActivity 的onCreate()已经实例化后,我的 ViewPager 适配器,我的集装箱越来越

下面是我的的onCreate()

 私人MyAdapter mAdapter;
私人ViewPager mPager;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);的setContentView(R.layout.activity_main);mAdapter =新MyAdapter(getSupportFragmentManager());    mPager =(ViewPager)findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);    mContainerView =(ViewGroup中)findViewById(R.id.container);
    //这里mContainerView已经是空
    ...
}

下面是片段这是一部分的 ViewPager 包含 mContainerView

 <?XML版本=1.0编码=UTF-8&GT?;
<的FrameLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / content_frame
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent><滚动型机器人:layout_width =match_parent
    机器人:layout_height =match_parent>
    <! - 这是我的ViewGroup - >
   <的LinearLayout机器人:ID =@ + ID /容器
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:方向=垂直
        机器人:showDividers =中间
        机器人:分=?机器人:dividerHorizo​​ntal
        机器人:animateLayoutChanges =真
        机器人:paddingLeft =16DP
        机器人:paddingRight =16DP/>< /滚动型>< TextView的机器人:ID =@机器人:ID /空
    风格=机器人:textAppearanceSmall
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_gravity =中心
    机器人:填充=32dp
    机器人:文字=@字符串/ message_empty_layout_changes
    机器人:文字颜色=?机器人:textColorSecondary/>< /&的FrameLayout GT;


解决方案

如果我正确地读你的问题,你要访问片段使用活动视图(儿童) findViewById()方法。这不应该因为片段膨胀自己的布局工作,片段是的的传统查看

如果您知道片段被实例化,你可以找回,你可以得到的一个实例的ViewGroup 使用

  yourFragment#getView()。findViewById()

如果没有,你可以让你的活动实现与接受的ViewGroup 作为一个方法的接口论据。然后在片段的 onCreateView(),有片段通过的ViewGroup 关闭的接口。您可以直接投射到您的活动,而是一个接口是清洁。

例如:

 公共类片段{ 公共接口ViewGroupCreateListener {     公共无效onViewGroupCreated(ViewGroup中V);
 } 私人ViewGroupCreateListener侦听器; 公共无效onAttach(活动){
     super.onAttach(一);
     监听=(ViewGroupCreateListener)一个;
 }公共查看onCreateView(/ *所有的参数在这里* /){
    视图V = inflater.inflate(R.layout.your_layout);
    ViewGroup中组= v.findViewById(R.id.container);
    listener.onViewGroupCreated(组);    返回伏;
 }

}

活动看起来是这样的:

 公共类MainActivity扩展活动实现ViewGroupCreateListener,OtherInterface1,OtherInterface2 {  私人的ViewGroup mViewGroup;
  公共无效onViewGroupCreated(ViewGroup中V){
     mViewGroup = V;
  }
}

这是好的,因为当寻呼机重新实例化片段,活动仍然得到的一个有效的实例的ViewGroup

或者的,如果取决于你实际上试图实现这种的ViewGroup ,你可以做内部的这种处理片段本身。

I have a Fragment that is part of a ViewPager. In this Fragment I have a ViewGroup child. Now, why in my MainActivity's onCreate() after having instantiated my ViewPager and adapter, my Container is getting null?

Here is my 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
    ...
}

Here is the Fragment that is part of a ViewPager that contains mContainerView

<?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>
解决方案

If I'm reading your question correctly, you're trying to access the Fragment's View (and children) using the Activity's findViewById() method. That shouldn't work since the Fragment inflates its own layout and Fragments are not traditional Views.

If you know the Fragment is instantiated and you can retrieve it, you can get an instance of the ViewGroup using

yourFragment#getView().findViewById()

If not, you can make an interface that your Activity implements with a method that accepts a ViewGroup as the argument. Then in the Fragment's onCreateView(), have the Fragment pass the ViewGroup off to the interface. You can cast directly to your Activity, but an interface is cleaner.

Eg

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

}

Your Activity would look something like:

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

  private ViewGroup mViewGroup;


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

This is nice since if the pager re-instantiates the Fragment, the Activity still gets a valid instance of the ViewGroup.

Or, if depending on what you're actually trying to achieve with this ViewGroup, you may be able to do this processing inside the Fragment itself.

这篇关于空的对象是一个片段的子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 13:28