本文介绍了在 VIewPager (Android) 中的两个 Fragment 之间传递数据 (NullPointerException)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以基本上我有 2 个片段 - FragmentConverterFragmentFavourites,我有一个 MainActivity.我正在尝试使用名为 Communicator 的接口将 4 个数组从第一个片段传递到第二个片段.具体片段如下:

So basically I have 2 Fragments - FragmentConverter and FragmentFavourites, and I have one MainActivity. I'm trying to pass 4 arrays from the first fragment to the second one using an Interface called Communicator. The specific snippets are show below:

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里面实现的接口方法:

This is the implemented interface method inside 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中收集数据的方法:

And this is a method that collects the data in 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 语句返回正确的结果.

Now the problem is that whenever I try to change a ui component inside FragmentConverter, I get a NullPointerException, though the Log.d statement returns the correct results.

FragmentPagerAdapter 的 getItem() 方法:

getItem() method of FragmentPagerAdapter:

@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 个片段的适配器,您可以尝试使用类似的方法:

When you call fragmentPagerAdapter.getItem(1) you are getting a new instance of the fragment so you are referring to a different object. this is why the view is null and you get the NullPointerException. If you need an adapter for only 2 fragments, you can try with something like that:

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;
            }
        }
    }

这篇关于在 VIewPager (Android) 中的两个 Fragment 之间传递数据 (NullPointerException)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 01:16