【问题标题】:findFragmentById returns null for Fragment that has been added using FragmentTransaction [duplicate]findFragmentById 为使用 FragmentTransaction 添加的 Fragment 返回 null [重复]
【发布时间】:2016-08-05 09:44:32
【问题描述】:

myFragment 和 myFragment2 都为以下代码返回 null。不确定是什么问题。 This solution 很遗憾没有解决问题,它不是重复的。

MainActivity

public class MainActivity extends AppCompatActivity {

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

    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    NonceInputFragment nonceInputFragment = new NonceInputFragment();
    transaction.add(R.id.nonce_input_fragment,nonceInputFragment,"test");
    transaction.commit();

    // It crashes on this line:
    getSupportFragmentManager().executePendingTransactions();

    NonceInputFragment myFragment = (NonceInputFragment) getSupportFragmentManager().findFragmentById(R.id.nonce_input_fragment);

    NonceInputFragment myFragment2 = (NonceInputFragment) getSupportFragmentManager().findFragmentByTag("test");
    }
}

nonce_input.xml

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/tvLabel"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

NonceInputFragment

public class NonceInputFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.nonce_input, container, false);
        return view;
    }
}

错误消息:
java.lang.RuntimeException:无法启动活动 ComponentInfo{name.com.viewpagercode/name.com.viewpagercode.MainActivity}:java.lang.IllegalArgumentException:找不到 id 的视图0x7f0b0056 (name.com.viewpagercode:id/nonce_input_fragment) 片段 NonceInputFragment{cece542 #0 id=0x7f0b0056 测试}

【问题讨论】:

  • FragmentTransactions 异步发生,因此当您尝试在该代码中找到您的 Fragment 时,它们不会完成。如果您确实需要在onCreate() 中获得对Fragment 的引用,请在commit() 呼叫之后立即在支持FragmentManager 上呼叫executePendingTransactions()
  • @MikeM。添加 executePendingTransactions 会使应用崩溃。
  • 它仍然是重复的,你现在又遇到了另一个问题。我不知道你在评论亚当的回答中到底是什么意思,但他是正确的(除了布局名称不匹配,现在你已经改变了)。您在add() 调用中传递的ID 必须是Activity 布局中ViewGroup 的ID,而不是Fragment 布局中的ID。
  • @MikeM。你说得对,我忘了片段必须属于一个活动。

标签: android android-fragments


【解决方案1】:

您的布局R.layout.identify_fragment 没有android:id="@+id/nonce_input_fragment" 的视图。

具体来说,这一行:

transaction.add(R.id.nonce_input_fragment,nonceInputFragment,"test");

是说“将此片段添加到此活动中,并将其放入ID为nonce_input_fragment的容器中。异常表明您的活动中不存在此类视图-意味着您的布局(identify_fragment)需要添加一些东西像这样:

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

【讨论】:

  • identify_fragment.xml(愚蠢的名字,应该是main_activity.xml)属于MainActivity。我需要调用NonceInputFragment中的一个函数,它属于不同的Activity。 NonceInputFragment 不是 identify_fragment.xml 的一部分
猜你喜欢
  • 1970-01-01
  • 2014-06-14
  • 2013-06-27
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-05
相关资源
最近更新 更多