就像我以前的帖子:Adding Integers from Different Fragments in a ViewPager

但是现在,我正在尝试在FragA的微调器中分配一个值,并将其添加到FragB的int中。像以前的帖子一样,总和将在FragC上。这是我的代码:

片段

public class FragA extends Fragment{

    Spinner spinner1;
    String s1;

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

        spinner1 = (Spinner) getActivity().findViewById(R.id.spinner1);
        spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {

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

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {

            }
        });
        return inflater.inflate(R.layout.fraga, container, false);
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

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

    public int getInt(String s1) {

        int a = 0;

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

        return a;
    }

}


片段B

public class FragB extends Fragment{

        int b = 20;

        public int getInt() {

            return b;
        }

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

            return inflater.inflate(R.layout.fragb, container, false);
        }

    }


主要活动

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();
                }
                if (i == 2)
                {
                    fragment = new FragC();
                }
                return fragment;
            }

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

    public String sum() {

        FragA FragA = new FragA();
        FragB FragB = new FragB();
        return Integer.valueOf(FragA.getInt() + FragB.getInt());
    }

}


片段C

public class FragC extends Fragment{

    Button button;
    TextView textView;

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

        View v = inflater.inflate(R.layout.fragc, container, false);

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

        return v;
    }

    OnClickListener Click = new OnClickListener() {

        @Override
        public void onClick(View v) {

            textView.setText(((MainActivity)getActivity()).sum());

        }
    };

}


MainActivity getInt()出现错误,指出“ FragA类型的方法getInt()不适用于arguments()”

最佳答案

将此行放在onCreateView中

spinner1 = (Spinner) getActivity().findViewById(R.id.spinner1);

07-24 09:37