0

I already saw some examples but I cant use them because they create a new instance of the fragment.

When I click a button in MainActivity it load a FragmentActivity the loads 3 Fragments.

I want to pass ip and port from the MainActivity to the Fragment_B.

My way is not working:

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

MainActivity:

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);


        }
    });

}

Fragment Activity(this loads all the fragments)

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);


    }


}

FRAGMENT B

  @Override
   public void onViewCreated(View view, @Nullable Bundle savedInstanceState)     {

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

}
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95

1 Answers1

0

Pass values in the intent from MainActivity to Main2Activity

args.putString("IP",ip);
args.putInt("PORTA",porta);

like this

Intent i = new Intent(MainActivity.this,Main2Activity.class);
i.putExtra("IP",ip);
i.putExtra("PORTA",porta);
startActivity(i);

then recive in Main2Activity

String ip = getIntent().getStringExtra("IP");
String porta = getIntent().getStringExtra("PORTA");

here add it in fargment and then use the fragment in your view pager.

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

Look here its full example

Community
  • 1
  • 1
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41