【问题标题】:Fragment onCreateView is never called片段 onCreateView 永远不会被调用
【发布时间】:2017-06-16 22:26:44
【问题描述】:

我注意到我的一个片段的onCreateView() 从未被调用,我不知道为什么。我需要能够在onResume() 中进行更改,但那也从未被调用。

这是我添加片段的方法。

public class LiveGameStats extends FragmentActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_live_game_stats);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction().add(R.id.tab1, new Tab1Fragment()).commit();
    }

    FragmentTabHost mTabHost = (FragmentTabHost) findViewById(R.id.liveGameTabhost);
    mTabHost.setup(LiveGameStats.this, getSupportFragmentManager(), R.id.liverealtabcontent);
    mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("You"),
            LiveGameTab1Fragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Your Team"),
            LiveGameTab2Fragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Your Enemies"),
            LiveGameTab3Fragment.class, null);
}

Tab1片段

public class Tab1Fragment extends Fragment {

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View V = inflater.inflate(R.layout.tab1, container, false);
    Log.v("FRAGMENT","FragmentActivityCreateView");
    return V;
}

最后是R.layout.tab1下传递的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context=".DeviceFragment" >

  <ImageButton
    android:id="@+id/searchBtn"
    android:src="@drawable/waves"
    android:background="@drawable/search_selector"
    android:padding="40dp"
    android:layout_height="200dp"
    android:layout_width="200dp"
    android:onClick="liveMatch"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />
</RelativeLayout>

我想我可能以错误的方式创建了片段。正如您现在所看到的,它们是按照 xml 的说明创建的,但是当我更改选项卡时,内容被“重置”。我在 onCreateView()onResume() 中放置的任何更改都不会被调用。

编辑

我已尝试实现第 1 条评论中描述的方法。实施后,选项卡具有相同的内容。就像所有选项卡都有相同的内容。这次我发布了我所有的课程及其相关的 XML。

public class LiveGameStats extends FragmentActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_live_game_stats);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent, new LiveGameTab1Fragment()).commit();
        getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent, new LiveGameTab2Fragment()).commit();
        getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent, new LiveGameTab3Fragment()).commit();

    }

    FragmentTabHost mTabHost = (FragmentTabHost) findViewById(R.id.liveGameTabhost);
    mTabHost.setup(LiveGameStats.this, getSupportFragmentManager(), R.id.liverealtabcontent);
    mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("You"),
            LiveGameTab1Fragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Your Team"),
            LiveGameTab2Fragment.class, null);
    mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Your Enemies"),
            LiveGameTab3Fragment.class, null);
}

activity_live_game_stats.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_live_game_stats"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@android:color/holo_blue_light"
  tools:context="lucian.leagueassistant.activity.LiveGameStats">


  <android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/liveGameTabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TabWidget
            android:id="@android:id/tabs"

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"
            android:orientation="horizontal" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />

        <FrameLayout
            android:id="@+id/liverealtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

    </LinearLayout>
  </android.support.v4.app.FragmentTabHost>
</LinearLayout>

我不会发布来自LiveGameTab2LiveGameTab3 的代码,因为它们几乎相同。我还有另一项活动

编辑2

当我从 getSupportFragmentManager().beginTransaction().add(R.id.liverealtabcontent, new LiveGameTab1Fragment()).commit(); 更改为 getSupportFragmentManager().beginTransaction().add(android.R.id.tabcontent, new LiveGameTab1Fragment()).commit(); 时,它似乎解决了

【问题讨论】:

    标签: android android-fragments android-lifecycle


    【解决方案1】:

    问题是这样的:

    getSupportFragmentManager().beginTransaction().add(R.id.tab1, new Tab1Fragment()).commit();
    

    在第一个参数中,您必须将 id 传递给 Activity 布局中将附加片段的容器。

    您正在传递整个布局R.id.tab1

    activity_live_game_statsxml文件中添加一个容器视图,

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      tools:context=".DeviceFragment" >
    
       <FrameLayout
        android:id="@+id/myContainer"
        ...(Other attrs)>
        </FrameLayout>
    
    </RelativeLayout>
    

    然后

      if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction().add(R.id.myContainer, new Tab1Fragment()).commit();
        }
    

    【讨论】:

    • 我在尝试您的方法的地方发布了一个编辑。你能看看它并在这里发布吗
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 2013-10-12
    • 2012-03-27
    相关资源
    最近更新 更多