Error using Mainactivity method in Fragment

Asked

Viewed 66 times

0

I am trying to use a method that returns a list, when using this same method inside the mainactivity where it was created everything works well, but 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;
}

How am I trying to call you in 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;
}
  • 1

    As already answered in 2 other questions about this same list you made. You have to first make the list query call and once it is filled in you create the Fragment, or you have to fill in the list within the Fragment

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);
}
}
  • worked, however when the app starts q it opens the Fragment the first time it error ( however the app continues to work ), only works when I click the button that makes the fragment open again for the second time, hence it returns me the list size. see:

  • 06-24 15:20:53.939 15778-15778/? d/logzin: called api 06-24 15:20:53.939 15778-15778/? D/Logzin: put the playlist on the mplaylist 06-24 15:20:53.939 15778-15778/? D/Logzin: catch java.lang.NullPointerException&#xA;06-24 15:20:59.349 15778-15778/br.com.igoroliv.youtubecanal D/Logzin: chamou api&#xA;06-24 15:20:59.349 15778-15778/br.com.igoroliv.youtubecanal D/Logzin: colocou a playlist na mplaylist&#xA;06-24 15:20:59.349 15778-15778/br.com.igoroliv.youtubecanal D/Logzin: List Size 5

Browser other questions tagged

You are not signed in. Login or sign up in order to post.