Android将数据从活动传递到片段而不创建新实例

时间:2016-08-21 12:36:03

标签: java android android-fragments

我已经看过一些例子,但我不能使用它们,因为它们会创建片段的新实例。

当我单击MainActivity中的按钮时,它会加载FragmentActivity,加载3个片段。

我想将ip和端口从MainActivity传递到Fragment_B。

我的方式不起作用:

  

java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.String android.os.Bundle.getString(java.lang.String)'

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


        }
    });

}

片段活动(这会加载所有片段)

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

}

1 个答案:

答案 0 :(得分:0)

将意图中的值从MainActivity传递到Main2Activity

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

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

然后在Main2Activity

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

此处将其添加到fargment中,然后在视图分页器中使用该片段。

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