无法将数据从 activity 发送到带有捆绑包的片段
Cant send data from activity to fragment with bundle
我有一个选项卡布局,我想将我的 ID 发送到我的片段中。我试过使用 Bundle 但它不起作用。看来我放错了代码。这是代码。
我的Activity
public class DetailPegawai extends AppCompatActivity {
private ViewPager mViewPager;
private SectionsPagerAdapter mSectionsPageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_pegawai);
getSupportActionBar().hide();
mSectionsPageAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(mViewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
private void setupViewPager(ViewPager viewPager) {
String id = getIntent().getStringExtra("id");
SectionsPagerAdapter adapter = new SectionsPagerAdapter(getSupportFragmentManager());
Bundle bundle = new Bundle();
bundle.putString("id", id);
Tab_1_Workplace fragobj = new Tab_1_Workplace();
fragobj.setArguments(bundle);
adapter.addFragment(new Tab_1_Workplace(), "WORKPLACE");
adapter.addFragment(new Tab_2_PersonalInfo(), "INFO");
viewPager.setAdapter(adapter);
}
我的片段
public class Tab_1_Workplace extends Fragment{
String id;
View rootView;
public Tab_1_Workplace() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView=inflater.inflate(R.layout.tab_1_workplace, container, false);
bundle = this.getArguments();
id = bundle.getString("id");
return rootView;
}
分区寻呼机适配器
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
Logcat错误
java.lang.String android.os.Bundle.getString(java.lang.String)' on a
null object reference
您已设置捆绑包。但问题是你永远不会发送它。你必须用你的包调用你的片段,而不是你的默认片段。
adapter.addFragment(fragobj, "WORKPLACE");
所以你的代码一定是这样的
public class DetailPegawai extends AppCompatActivity {
private ViewPager mViewPager;
private SectionsPagerAdapter mSectionsPageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_pegawai);
getSupportActionBar().hide();
mSectionsPageAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(mViewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
private void setupViewPager(ViewPager viewPager) {
String id = getIntent().getStringExtra("id");
SectionsPagerAdapter adapter = new SectionsPagerAdapter(getSupportFragmentManager());
Bundle bundle = new Bundle();
bundle.putString("id", id);
Tab_1_Workplace fragobj = new Tab_1_Workplace();
fragobj.setArguments(bundle);
adapter.addFragment(fragobj, "WORKPLACE");
adapter.addFragment(new Tab_2_PersonalInfo(), "INFO");
viewPager.setAdapter(adapter);
}
我有一个选项卡布局,我想将我的 ID 发送到我的片段中。我试过使用 Bundle 但它不起作用。看来我放错了代码。这是代码。
我的Activity
public class DetailPegawai extends AppCompatActivity {
private ViewPager mViewPager;
private SectionsPagerAdapter mSectionsPageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_pegawai);
getSupportActionBar().hide();
mSectionsPageAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(mViewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
private void setupViewPager(ViewPager viewPager) {
String id = getIntent().getStringExtra("id");
SectionsPagerAdapter adapter = new SectionsPagerAdapter(getSupportFragmentManager());
Bundle bundle = new Bundle();
bundle.putString("id", id);
Tab_1_Workplace fragobj = new Tab_1_Workplace();
fragobj.setArguments(bundle);
adapter.addFragment(new Tab_1_Workplace(), "WORKPLACE");
adapter.addFragment(new Tab_2_PersonalInfo(), "INFO");
viewPager.setAdapter(adapter);
}
我的片段
public class Tab_1_Workplace extends Fragment{
String id;
View rootView;
public Tab_1_Workplace() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView=inflater.inflate(R.layout.tab_1_workplace, container, false);
bundle = this.getArguments();
id = bundle.getString("id");
return rootView;
}
分区寻呼机适配器
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
@Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList.size();
}
Logcat错误
java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
您已设置捆绑包。但问题是你永远不会发送它。你必须用你的包调用你的片段,而不是你的默认片段。
adapter.addFragment(fragobj, "WORKPLACE");
所以你的代码一定是这样的
public class DetailPegawai extends AppCompatActivity {
private ViewPager mViewPager;
private SectionsPagerAdapter mSectionsPageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail_pegawai);
getSupportActionBar().hide();
mSectionsPageAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.container);
setupViewPager(mViewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
}
private void setupViewPager(ViewPager viewPager) {
String id = getIntent().getStringExtra("id");
SectionsPagerAdapter adapter = new SectionsPagerAdapter(getSupportFragmentManager());
Bundle bundle = new Bundle();
bundle.putString("id", id);
Tab_1_Workplace fragobj = new Tab_1_Workplace();
fragobj.setArguments(bundle);
adapter.addFragment(fragobj, "WORKPLACE");
adapter.addFragment(new Tab_2_PersonalInfo(), "INFO");
viewPager.setAdapter(adapter);
}