在VIewPager(Android版)(NullPointerException异常)传递两个片段之间的数据 [英] Passing data between two Fragments in a VIewPager (Android) (NullPointerException)

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

问题描述

所以基本上我的 2 片段 - FragmentConverter FragmentFavourites ,我有有一个 MainActivity 。我试图通过从第一片段4阵列使用名为通讯接口的第二个。具体的片段是下面显示:

 公共接口通讯{    公共无效响应(字符串[]名称,字符串[] codeS,的String []符号,INT []图像);
}

这是内部的方法 FragmentFavourites

  @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        // TODO自动生成方法存根        的String [] = checkedNames新的String [窗口];
        的String []检查codeS =新的String [窗口];
        的String [] = checkedSymbols新的String [窗口];
        INT [] = checkedImages新INT [窗口];
        COMM =(通讯)getActivity();
        INT索引= 0;
        如果(item.getItemId()== R.id.action_save){
            的for(int i = 0; I< checked.size();我++){
                如果(checked.get(我)==真){
                    checkedNames [指数] =名称[I]
                    检查codeS [指数] = codeS [I]
                    checkedSymbols [指数] =符号[I]
                    checkedImages [指数] =图像[我]
                    指数++;
                }            }
            comm.respond(checkedNames,检查codeS,checkedSymbols,checkedImages);
        }        返回super.onOptionsItemSelected(项目);
    }

这是在 MainActivity 实现的接口方法:

  @覆盖
公共无效响应(字符串[]名称,字符串[] codeS,的String []符号,
        INT []图像){
    // TODO自动生成方法存根
    FragmentConverter断枝=(FragmentConverter)fragmentPagerAdapter.getItem(1);
    frag.changeData(姓名,codeS,符号,图像);
}

这是收集数据的方法 FragmentConverter

 公共无效changeData(字符串[]名称,字符串[] codeS,的String []符号,INT []图像){
        this.names =名称;
        这codeS = codeS;
        this.symbols =符号;
        this.images =图像;        Log.d(TEST,symbols.length + names.length + codes.length + images.length +);
        tvOneRate.setText(名称[1]);
}

现在的问题是,每当我试图改变在 FragmentConverter UI组件,我得到一个 NullPointerException异常,虽然Log.d语句返回正确的结果。

EDIT1:getItem()时FragmentPagerAdapter方式:

  @覆盖
    公共片段的getItem(int i)以{
        // TODO自动生成方法存根
        片段FRAG = NULL;
        如果(我== 0){
            FRAG =新FragmentFavourites();
        }
        如果(ⅰ== 1){
            FRAG =新FragmentConverter();
        }
        返回FRAG;
    }


解决方案

编辑:

当你调用 fragmentPagerAdapter.getItem(1)你得到,所以你指的是一个不同的对象片段的新实例。这就是为什么认为是空,你会得到NullPointerException异常。如果你需要的只是2片段的适配器,可以用类似的东西尝试:

 公共类YourPagerAdapter扩展android.support.v4.app.FragmentPagerAdapter {        私人FragmentFavourites mFragFavourites;
        私人FragmentConverter mFragConverter;        公共YourPagerAdapter(){            // ...您code以上
            this.mFragFavourites =新FragmentFavourites();
            this.mFragConverter =新FragmentConverter();
        }        @覆盖
        公共片段的getItem(INT位置){            开关(位置){
                情况下0:
                    返回mFragFavourites;                情况1:
                    返回mFragConverter;
                默认:
                    返回null;
            }
        }
    }

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

This is a method inside 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);
    }

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

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

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.

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

解决方案

EDITED:

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版)(NullPointerException异常)传递两个片段之间的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆