Android 将字符串从活动传递给片段

Android 将字符串从活动传递给片段,android,android-fragments,Android,Android Fragments,我需要从活动类向片段传递一些值。 我试图将包传递给片段,但结果为空。 我需要接收这些值,然后在片段活动中运行AsyncTask 代码片段FirstFragment.java: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { v = inflater.inflate(R.layout.first_frag

我需要从活动类向片段传递一些值。 我试图将包传递给片段,但结果为空。 我需要接收这些值,然后在片段活动中运行AsyncTask

代码片段FirstFragment.java:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.first_frag, container, false);
        Bundle bundle = getArguments();
        if(bundle!=null)
        {
            sensorUID=getArguments().getString("senID");
            System.out.println("we got this: " + sensorUID);
        }
        System.out.println("we got this2: "+ sensorUID);
        new MyAsyncTask().execute();
        return v;
    }
代码(活动类):

然后,您可以使用`getArguments.getString(“sendID”);检索片段中的参数

或者您可以在newInstance中将您的ID作为参数传递,如下所示
fragment=FirstFragment.newInstance(senId)
在新实例中,您将数据从
newInstance
传递到
onCreateview
,如下所示:

 public static Fragment newInstance(String senID){
   FirstFragmentf = new FirstFragment();

   Bundle args = new Bundle();
   args.putString("senID", sendID);
   f.setArguments(args);

    return f;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    sensorID= getArguments().getString("sendID");

}

在片段中创建一个字段:

public Bundle bundle;
在主活动中,将您的包分配给

f.bundle=b;
删除

Bundle bundle=getArguments();
在你的活动中:

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

        //Actiity to Fragment
        Bundle bundle = new Bundle();
        bundle.putString("test", "From Activity");
        // set Fragmentclass Arguments
        TestFragment testFragment = new TestFragment();
        testFragment.setArguments(bundle);
        testFragment.myTest();

    }
在你的片段中:

public void myTest(){
        //Get String from Activity
        String stringText = getArguments().getString("test");
        Log.d(LOG_TAG, "The String from the Activity = " + stringText);
    }

看看这里:@TheCrafter我确实试过了。它找不到intentDin的putInt或putString无效。我甚至不能正确地接触到碎片,现在我忘记了断裂;在开关里。你确定你正确地实例化了片段吗?成功了,我添加了中断。修复了一些乱七八糟的键名,它终于起作用了。:)
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Actiity to Fragment
        Bundle bundle = new Bundle();
        bundle.putString("test", "From Activity");
        // set Fragmentclass Arguments
        TestFragment testFragment = new TestFragment();
        testFragment.setArguments(bundle);
        testFragment.myTest();

    }
public void myTest(){
        //Get String from Activity
        String stringText = getArguments().getString("test");
        Log.d(LOG_TAG, "The String from the Activity = " + stringText);
    }