将一个Spinner的赋值传递给Viewpager NullPointerException中的Fragment到Fragment

时间:2013-12-18 17:19:22

标签: java android android-fragments android-viewpager android-spinner

我根据上一篇文章中的答案更简单并更新了它:Adding Assigned Values in Spinner NullPointerException

我有一个使用 ViewPager MainAcitivty 。我的MainActivity中有2个片段(FragA和FragB)

在我的 FragA 中,我有一个微调器。在 FragB 中,我有 TextView 按钮

现在我要做的是,当我在我的微调器中选择“Hello”时,我的int将具有值5.当我单击Button时,5将显示在TextView中。

这是我的代码:

弗拉加

public class FragA extends Fragment {

    Spinner spinner1;
    String s1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fraga, container, false); 

        spinner1 = (Spinner) view.findViewById(R.id.spinner1);  
        ArrayAdapter<CharSequence>  adapter_a = ArrayAdapter.createFromResource(getActivity(), R.array.spinner1,android.R.layout.simple_spinner_item );
        spinner1.setAdapter(adapter_a);

        s1 = spinner1.getSelectedItem().toString();

        return view;
    }

    public int getInt() {

        int a = 0;

        if(s1.equals("Hello")) {
            a = 5;
        }

        return a;    
    }

}

MainActivity

public class MainActivity extends FragmentActivity {

    ViewPager viewPager = null; 

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

        viewPager = (ViewPager)findViewById(R.id.pager);    
        FragmentManager fragmentManager = getSupportFragmentManager();
        viewPager.setAdapter(new MyAdapter(fragmentManager));
    }

    public class MyAdapter extends FragmentStatePagerAdapter {  

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

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

                if (i == 0)
                {
                    fragment = new FragA();
                }
                if (i == 1)
                {
                    fragment = new FragB();
                }
                return fragment;
            }

            @Override
            public int getCount() {
                return 2;
            }   
        }

    public String get() {

        FragA FragA = new FragA();

        return Integer.toString(FragA.getInt());
    }
}

FragB

public class FragB extends Fragment{

    TextView textView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragb, container, false); 

        textView = (TextView) view.findViewById(R.id.textview);
        Button button = (Button) view.findViewById(R.id.button);
        button.setOnClickListener(Click);

        return view;
    }

    OnClickListener Click = new OnClickListener() { 

        @Override
        public void onClick(View v) {

            textView.setText(((MainActivity)getActivity()).get());          
        }
    };

}

顺便说一句,当这是我的代码时,我将从FragA的值传递给FragB没有问题:

public int getInt() {

        int a = 5;

        return a;    
    }

但是,它并不涉及我的微调器,这不是我想做的。

1 个答案:

答案 0 :(得分:0)

这是片段之间进行通信的简单方法

在MainActivity中,保留片段的静态实例。

public static FragA fragmentA;
public static FragB fragmentB;

现在,如果您想从FragA访问FragB,请编写类似的内容:

((MainActivity) getActivity()).fragmentB.setSomething();

以下是片段之间进行通信的更好/正确的方式

http://developer.android.com/training/basics/fragments/communicating.html