Error when using MainActivity method in a fragment [closed]

0

I'm trying to use a method that returns a list, when I use this same method inside the mainactivity where it was created everything works fine, however when trying to access it in a fragment of the NullPointerException error.

log:

06-24 14:29:11.829 32645-32645/? D/Logzin: chamou api
06-24 14:29:11.839 32645-32645/? D/Logzin: colocou a playlist na mplaylist
06-24 14:29:11.839 32645-32645/? D/Logzin: catch java.lang.NullPointerException

                                           [ 06-24 14:29:11.859 32645:32645 D/         ]
                                           HostConnection::get() New Host Connection established 0xb98ce920, tid 32645


                                           [ 06-24 14:29:11.869 32645:32645 W/         ]
                                           Unrecognized GLES max version string in extensions: ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 

Method on Main:

public  List<PlaylistItem> getlistavideos(){

    try {
        mplaylistItems = playlistItems;
        Log.d(TAG,"colocou a playlist na mplaylist");

    }catch (Exception e){
        Log.d(TAG,"Catch Main " + e);
    }


    return mplaylistItems;
}

As I'm trying to call it in the fragment:

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

    View v = inflater.inflate(R.layout.fragmentlistavideos,container,false);
    /*mList = new ArrayList<String>();
    mList.add(mList.size(),"Um");
    mList.add(mList.size(),"Dois");
    */

    try {
        int tamanho= ((MainActivity)getActivity()).getlistavideos().size();
        Log.d(TAG,"Tamanho da Lista "+ tamanho);
    }catch (Exception e){
        Log.d(TAG,"catch "+e);
    }


    return v;
}
    
asked by anonymous 24.06.2017 / 16:35

1 answer

1

try to run your code inside the onActivityCreated event

    @Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
        try {
    int tamanho= ((MainActivity)getActivity()).getlistavideos().size();
    Log.d(TAG,"Tamanho da Lista "+ tamanho);
}catch (Exception e){
    Log.d(TAG,"catch "+e);
}
}
    
24.06.2017 / 17:13