片段到片段接口错误(布局) [英] Fragment to Fragment Interface error (layout)

查看:90
本文介绍了片段到片段接口错误(布局)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过主要活动从片段5到3发送接口:

I'm sending an interface from Fragment 5 to 3 through the main activity :

public void setF3Riddle(int x) {

  Frag3 F3 = (Frag3) getSupportFragmentManager().findFragmentById(R.id.Frag3);

  if (F3 != null) {
     F3.getF3Riddle(x);
  } else {
     Frag3 frag = new Frag3();
     Bundle args = new Bundle();
     args.putInt("Value", x);
     frag.setArguments(args);
     getSupportFragmentManager().beginTransaction()
                                .replace(R.id.Frag3, frag)
                                .addToBackStack(null)
                                .commit();
     frag.getF3Riddle(x);
  }
}

但是在运行时发送数据时崩溃了。当我将 .replace(R.id.Frag3,Frag)容器更改为另一个不崩溃的片段时,我注意到了这个问题。这有什么问题?

But it crashes when I send the data while running. I've noticed the problem when I change the .replace(R.id.Frag3,Frag) container to another fragment it doesn't crash. What's the problem here?

片段3布局:

<FrameLayout 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:id="@+id/Frag3"
    tools:context="com.redot.puzzle3.Frag3">

非常感谢您在这件事上的时间和帮助。

Thank you very much for your time and assistance in this matter.

推荐答案

问题可能来自这里:

Frag3 frag = new Frag3();
Bundle args = new Bundle();
args.putInt("Value", x);
frag.setArguments(args);
getSupportFragmentManager().beginTransaction()
                           .replace(R.id.Frag3, frag)
                           .addToBackStack(null)
                           .commit();
frag.getF3Riddle(x);

您正在使用创建片段Frag3 frag = new Frag3(); 然后放入包然后你想用 frag.getF3Riddle(x)获取值。但它会抛出一个错误,因为片段尚未完成创建。片段创建是异步过程。因此,您需要等到Frag完成创建或首先在另一个地方创建它。

You're creating the fragment with Frag3 frag = new Frag3(); and put the bundle then you want to get the value with frag.getF3Riddle(x). But it will throw an error because the the fragment is not yet finished created. Fragment creation is asynchronous process. So, you need to wait until the frag is finished created or create it in another place first.

这篇关于片段到片段接口错误(布局)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆