所以基本上我有两个片段-FragmentConverterFragmentFavourites,我有一个MainActivity。我试图使用名为Communicator的接口将4个数组从第一个片段传递到第二个片段。具体代码片段如下所示:

public interface Communicator {

    public void respond(String[] names, String[] codes, String[] symbols, int[] images);
}

这是FragmentFavourites中的一种方法:
@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub

        String[] checkedNames = new String[counter];
        String[] checkedCodes = new String[counter];
        String[] checkedSymbols = new String[counter];
        int[] checkedImages = new int[counter];
        comm = (Communicator) getActivity();
        int index = 0;
        if (item.getItemId() == R.id.action_save){
            for (int i=0;i<checked.size();i++){
                if (checked.get(i) == true){
                    checkedNames[index] = names[i];
                    checkedCodes[index] = codes[i];
                    checkedSymbols[index] = symbols[i];
                    checkedImages[index] = images[i];
                    index++;
                }

            }
            comm.respond(checkedNames, checkedCodes, checkedSymbols, checkedImages);
        }

        return super.onOptionsItemSelected(item);
    }

这是MainActivity中实现的接口方法:
@Override
public void respond(String[] names, String[] codes, String[] symbols,
        int[] images) {
    // TODO Auto-generated method stub
    FragmentConverter frag = (FragmentConverter) fragmentPagerAdapter.getItem(1);
    frag.changeData(names, codes, symbols, images);
}

这是一种收集FragmentConverter中数据的方法:
public void changeData(String[] names, String[] codes, String[] symbols, int[] images){
        this.names = names;
        this.codes = codes;
        this.symbols = symbols;
        this.images = images;

        Log.d("TEST", symbols.length + names.length + codes.length + images.length + "");
        tvOneRate.setText(names[1]);
}

现在的问题是,每当我试图在FragmentConverter中更改ui组件时,都会得到一个NullPointerException,尽管log.d语句返回正确的结果。
Edit1:FragmentPageRadapter的getitem()方法:
@Override
    public Fragment getItem(int i) {
        // TODO Auto-generated method stub
        Fragment frag = null;
        if (i == 0){
            frag = new FragmentFavourites();
        }
        if (i == 1){
            frag = new FragmentConverter();
        }
        return frag;
    }

最佳答案

编辑:
当您调用fragmentPagerAdapter.getItem(1)时,您将获得片段的一个新实例,因此您引用的是另一个对象。这就是为什么视图为空,而您得到了nullpointerexception。如果您只需要2个片段的适配器,可以尝试以下方法:

public class YourPagerAdapter extends android.support.v4.app.FragmentPagerAdapter {

        private FragmentFavourites mFragFavourites;
        private FragmentConverter mFragConverter;

        public YourPagerAdapter() {

            // ... your code above
            this.mFragFavourites = new FragmentFavourites();
            this.mFragConverter = new FragmentConverter();
        }

        @Override
        public Fragment getItem(int position) {

            switch (position) {
                case 0:
                    return mFragFavourites;

                case 1:
                    return mFragConverter;
                default:
                    return null;
            }
        }
    }

10-02 06:12