将数据从Activity发送到Tab Fragment

时间:2016-08-11 04:53:17

标签: android android-fragments tabs

我的代码有轻微的问题,我在这个网站搜索但仍未找到最佳答案 我将数据发送到我的标签片段,这是我的代码

我的活动课

public class MainActivity extends AppCompatActivity 

    private ViewPager pager;
    private Toolbar toolbar;
    private TabLayout tabs;

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


        pager=(ViewPager) findViewById(R.id.pager);
        tabs=(TabLayout) findViewById(R.id.tabs);

        pager.setAdapter(new TabFragmentPagerAdapter(getSupportFragmentManager()));
        tabs.setupWithViewPager(pager);
        tabs.setTabGravity(TabLayout.GRAVITY_FILL);

        Bundle bundle = new Bundle();
        bundle.putString("edttext", "data From Activity");
        // set Fragmentclass Arguments
        tab1Fragment fragobj = new tab1Fragment();
        fragobj.setArguments(bundle);

    }
}

这是我的tab1类

public class tab1Fragment extends Fragment 

    public tab1Fragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View view= inflater.inflate(R.layout.fragment_tab1, container, false);
        TextView tes=(TextView) view.findViewById(R.id.testtab1);
        String strtext = getArguments().getString("edttext");

        tes.setText("tes :"+ strtext);
        return  view;
    }
}

和我的页面片段适配器

public class TabFragmentPagerAdapter extends FragmentPagerAdapter 

    String[] title = new String[]{
            "Tab 1", "Tab 2", "Tab 3"
    };

    public TabFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position){
        Fragment fragment=null;
        switch (position){
            case 0:
                fragment=new tab1Fragment();
                break;
            case 1:
                fragment=new tab2Fragment();
                break;
            case 2:
                fragment=new tab3Fragment();
                break;
            default:
                fragment=null;
                break;
        }
        return  fragment;
    }
    @Override
    public  CharSequence getPageTitle(int position){
        return  title[position];
    }
    @Override
    public  int getCount(){
        return  title.length;
    }
}

当我运行错误

  

显示java.lang.NullPointerException

请帮助

5 个答案:

答案 0 :(得分:5)

在您发送的AppCompatActivity

tab1Fragment fragobj = new tab1Fragment();
fragobj.setArguments(bundle);

但在您的TabFragmentPagerAdapter

fragment=new tab1Fragment();
            break;

它有空参数

内部开关

switch (position){
    case 0:
      fragment=new tab1Fragment();               
      Bundle bundle = new Bundle();
      bundle.putString("edttext", "data From Activity");
      fragment.setArguments(bundle);
    break;

答案 1 :(得分:1)

可能是你得到了NullPointerException,因为你的片段在getItem内没有参数再次初始化。

试试这种方式。

@Override
public Fragment getItem(int position){
    Fragment fragment=null;

    Bundle bundle = new Bundle();
    bundle.putString("edttext", "data From Activity");

    switch (position){
        case 0:
            fragment=new tab1Fragment();
            fragment.setArguments(bundle);
            break;
        case 1:
            fragment=new tab2Fragment();
            break;
        case 2:
            fragment=new tab3Fragment();
            break;
        default:
            fragment=null;
            break;
    }
    return  fragment;
}

注意:确保在MainActivity和tab1Fragment中导入相同版本的Fragment。

答案 2 :(得分:0)

我认为你首先使用Youractivity的On调用启动Tab1Frament()你可以这样做......

  

用过这个..

   if (savedInstanceState == null) {

 FragmentTransaction ft = this.getSupportFragmentManager().beginTransaction();

tab1Fragment fragobj = new tab1Fragment();

  Bundle bundle = new Bundle();
  bundle.putString("edttext", "data From Activity");
 // set Fragmentclass Arguments

 fragobj.setArguments(bundle);


    ft.replace(R.id.frame, fragobj);
    ft.addToBackStack(null);
    ft.commit();
  }
  

而不是

 Bundle bundle = new Bundle();
 bundle.putString("edttext", "data From Activity");
 // set Fragmentclass Arguments
 tab1Fragment fragobj = new tab1Fragment();
 fragobj.setArguments(bundle);

答案 3 :(得分:0)

在片段中创建一个函数以传递活动中的数据,就像在getterssetters中为对象类所做的那样:

// Fragment
class TabFragment extends Fragment{

    public String myDataString;

    ...

    public void setTabInfo(String myActivityString){
        this.myDataString = myActivityString;
    }
}

// Activity

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

    ...

    TabFragment frag = new TabFragment();
    frag.setTabInfo("data From Activity");

}

我更喜欢这种方法,因为对于对象,在使用Bundle的情况下无需直接序列化对象即可直接传递数据

答案 4 :(得分:0)

也许它的答案很晚,但是有些东西对我有用:

将捆绑包发送到适配器:

  Bundle bundle = new Bundle();
  bundle.putString("edttext", "data From Activity");
  pager.setAdapter(new TabFragmentPagerAdapter(getSupportFragmentManager(), bundle));

在适配器中扩展构造函数:

Bundle bundle;

  public TabFragmentPagerAdapter(FragmentManager fm, Bundle bundle) {
    super(fm);
    this.bundle=bundle;
}

并将其发送到片段:

switch (position){
        case 0:
            fragment=new tab1Fragment();
            fragment.setArguments(bundle);
            break;

希望它会有所帮助。            }