Android Pass Data from Activity to Fragment Without creating a new Instance
<p>I already saw some examples but I cant use them because they create a new instance of the fragment.</p>

<p>When I click a button in MainActivity it load a FragmentActivity the loads 3 Fragments.</p>

<p>I want to pass ip and port from the MainActivity to the Fragment_B.</p>

<p>My way is not working:</p>

<blockquote>
  <p>java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference</p>
</blockquote>

<p>MainActivity:</p>

<pre><code>public class MainActivity extends Activity {

TextView response;
EditText editTextAddress, editTextPort;
Button buttonConnect, buttonClear;
public static String ip = "192.168.2.2";
public static int porta = 6000;


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

    editTextAddress = (EditText) findViewById(R.id.txt_ip);
    editTextPort = (EditText) findViewById(R.id.txt_porta);
    buttonConnect = (Button) findViewById(R.id.btn_conetar);
    //buttonClear = (Button) findViewById(R.id.clearButton);


    buttonConnect.setOnClickListener(new View.OnClickListener() {
        public void onClick(View arg0) {

            ip = editTextAddress.getText().toString();
            porta = Integer.parseInt(editTextPort.getText().toString());


            Fragmento_B f = new Fragmento_B();
            Bundle args = new Bundle();
            args.putString("IP",ip);
            args.putInt("PORTA",porta);
            f.setArguments(args);

            Intent i = new Intent(MainActivity.this,Main2Activity.class);
            startActivity(i);


        }
    });

}
</code></pre>

<p>Fragment Activity(this loads all the fragments)</p>

<pre><code>public class Main2Activity extends FragmentActivity {

private TabLayout mTabLayout;
private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main2);

    mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
    mViewPager = (ViewPager) findViewById(R.id.view_pager);

        mViewPager.setAdapter(new FragmentoAdapter(getSupportFragmentManager(),getResources().getStringArray(R.array.tiles_tab)));
        mViewPager.setOffscreenPageLimit(3);

        mTabLayout.setupWithViewPager(mViewPager);


    }


}
</code></pre>

<p><strong>FRAGMENT B</strong></p>

<pre><code>  @Override
   public void onViewCreated(View view, @Nullable Bundle savedInstanceState)     {

     String ip = getArguments().getString("IP");
     int porta = getArguments().getInt("PORTA");

}
</code></pre>