Android 将参数从活动转移到片段

Android 将参数从活动转移到片段,android,android-activity,fragment,Android,Android Activity,Fragment,我创建了如中所述的滑动选项卡,并希望将传输的数据用于进一步的内容。 我搜索了解决方案,但每次值都是空的 我在活动中使用了以下代码。该片段称为tab1_simplecall,framelayout的ID为tab1_片段: if (findViewById(R.id.tab1_fragment) != null){ if (savedInstanceState!=null) { return; } tab1 = new tab1

我创建了如中所述的滑动选项卡,并希望将传输的数据用于进一步的内容。 我搜索了解决方案,但每次值都是空的

我在活动中使用了以下代码。该片段称为tab1_simplecall,framelayout的ID为tab1_片段:

if (findViewById(R.id.tab1_fragment) != null){
        if (savedInstanceState!=null) {
            return;
        }
        tab1 = new tab1_simplecall();
        getSupportFragmentManager().beginTransaction().add(R.id.tab1_fragment, tab1);
    }

Fragment fragObj = new Fragment();
Bundle bundle = new Bundle();
bundle.putInt("ID", ID);
bundle.putString("time", time);
bundle.putString("worker", worker);
fragObj.setArguments(bundle);
在我的片段中,我尝试接收数据:

 private void loadBundle() {
            Bundle b = getArguments();
            Log.i("tab1", "Check if b != null");
            if (b != null) {
                    String name= b.getString("name");
                    Integer ID= b.getInt("ID");
                    String date= b.getString("date");
                    Log.i("tab1 Übergabe: ", "" + name+ " " + ID+ " " + date);
            }
            else
            {
                    Log.i("tab1", "B = null");
            }
    }

但每一次,bundle b都是空的。如何解决该问题?

您可以使用SharedReference在活动或片段之间发送数据。

我将以下代码移动到活动中onCreate的末尾。现在数据将被传输。到目前为止非常感谢:)

但几毫秒后,应用程序崩溃,并显示消息“IllegalArgumentException:找不到id的视图…用于片段…”

选项卡的“我的布局”中的代码段:

<LinearLayout
    android:orientation="vertical"
    android:id="@+id/tab1_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="30dp">


在我的活动中,我在Fragmentlayout中调用片段的id(tab1_片段)。但是为什么呢(

你是如何将你的片段附加到你的应用程序的?片段事务?你能编辑你的问题并包含那行代码吗?Sry,我不确定,你到底想要什么:/你已经实例化了一个
片段
对象,但在你的代码中的某个地方你必须将该片段附加到一个容器框架。你能发布那行代码吗?我想是这样的是缺少的部分。应该是什么样子?我添加了一个关于附件的代码段,但它仍然不起作用。
<LinearLayout
    android:orientation="vertical"
    android:id="@+id/tab1_fragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="30dp">