如何将一个String从一个活动传递到android中的选项卡式活动的片段

时间:2017-05-01 21:59:57

标签: android android-fragments android-tabbed-activity

在询问之前我已经完成了类似的问题并尝试了但没有成功,每次都以空指针异常结束。 我想要做的就是将字符串中的用户名从一个活动传递到我选项卡式活动的另一个片段。 这是我尝试过的,我的主要活动代码

Bundle bundle = new Bundle();
                bundle.putString("NAME", username);
                //PASS OVER THE BUNDLE TO OUR FRAGMENT
                ProfileFragment profileFragment = new ProfileFragment();
                profileFragment.setArguments(bundle);

                startActivity(new Intent(MainActivity.this, TabbedAct.class));

来自ProfileFragment的代码

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_profile, container, false);

    profilename = (TextView) rootView.findViewById(R.id.profile);

    String name= this.getArguments().getString("NAME");

    profilename.setText(name);

    return rootView;
}

1 个答案:

答案 0 :(得分:0)

这是因为ProfileFragment尚未显示。您正在启动一个Activity而不是片段。

首先,您需要使用额外值

来使用TabbedAct
Intent intent = new Intent(MainActivity.this, TabbedAct.class);
intent.putExtra("Name", username);
startActivity(intent);

然后,在TabbedAct活动中,您需要初始化ProfileFragment

String username = getIntent().getStringExtra("Name");
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragment(ProfileFragment.newInstance(username),"Tab1");
viewPagerAdapter.addFragment(Fragment2.newsIntance(),"Tab2");
viewPagerAdapter.addFragment(Fragment2.newsIntance(),"Tab3");
.....
viewpager.setAdapter(viewPagerAdapter);
viewpager.setCurrentItem(0);
viewPagerAdapter.notifyDataSetChanged();

来自ProfileFragment的代码

public static ProfileFragment newInstance(String username) {
    ProfileFragment f = new ProfileFragment();
    Bundle bdl = new Bundle();
    bdl.putString("NAME", username);
    f.setArguments(bdl);
    return f;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_profile, container, false);

    profilename = (TextView) rootView.findViewById(R.id.profile);

    String name= this.getArguments().getString("NAME");

    profilename.setText(name);

    return rootView;
}

因为你没有ViewPagerAdapter。你可以使用我的

public class ViewPagerAdapter extends FragmentStatePagerAdapter {
    private List<Fragment> mFragmentList;
    private List<String> mFragmentTitleList;

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
        mFragmentTitleList = new ArrayList<>();
        mFragmentList = new ArrayList<>();
    }

    @Override
    public Fragment getItem(int index) {
        return mFragmentList.get(index);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    @Override
    public int getItemPosition(Object object){
        return PagerAdapter.POSITION_NONE;
    }
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (position >= getCount()) {
            FragmentManager manager = ((Fragment) object).getFragmentManager();
            FragmentTransaction trans = manager.beginTransaction();
            trans.remove((Fragment) object);
            trans.commit();
        }
    }

    public void addFragment(Fragment fragment,String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}