简体   繁体   中英

FragmentTransaction is throwing null reference

I have loader fragment and I am calling another fragment from the loader fragment .Everything is working fine but suddenly my fragment loader is giving me an error.In the loader fragment I am displaying progress bar for 5sec and after that another fragment will display.

Here is my code:-

loader fragment.java

package com.example.user.attendance;
public class Loader_fragment extends Fragment {
    AVLoadingIndicatorView Loader;
    Runnable runnable;
    Handler handler;
    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;
    Timer timer;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View loadder_fragment=inflater.inflate(R.layout.fragment_loader_fragment, container, false);

        Loader=(AVLoadingIndicatorView)loadder_fragment.findViewById(R.id.loder_login);
        handler=new Handler();
        runnable=new Runnable() {
            @Override
            public void run() {
                Loader.setVisibility(View.INVISIBLE);
                timer.cancel();
                fragmentManager=getFragmentManager();
                fragmentTransaction=fragmentManager.beginTransaction();
                fragmentTransaction.setCustomAnimations(R.animator.slide_in_left,R.animator
                        .slide_out_left,R.animator.slide_in_right,R.animator.slide_out_right);
                Home_Screen home_screen=new Home_Screen();
                fragmentTransaction.replace(R.id.Home_screen_fragment,home_screen);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();

            }


        };

        timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.post(runnable);
            }
        }, 5000,5000);


       /* if(Loader.setVisibility(View.INVISIBLE)==1){


        }*/

        return loadder_fragment;
    }


}

Logcat is showing an error:-

 Process: com.example.user.attendance, PID: 1200
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.FragmentTransaction android.app.FragmentManager.beginTransaction()' on a null object reference
        at com.example.user.attendance.Loader_fragment$1.run(Loader_fragment.java:44)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:7402)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Why it is showing null object reference in the fragment.

Don't perform View manipulation in onCreateView . Instead, move it to onViewCreated method. Reason: your fragment has not yet setup the views.

Besides, replace:

fragmentManager = getFragmentManager();

with the following:

fragmentManager = getChildFragmentManager();

Try this

FragmentTransaction fragmentTransaction = getActivity()
                    .getFragmentManager().beginTransaction();
fragmentTransaction.setCustomAnimations(R.animator.slide_in_left, 
                    R.animator.slide_out_left, 
                    R.animator.slide_in_right, 
                    R.animator.slide_out_right);
fragmentTransaction.replace(R.id.Home_screen_fragment, home_screen);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM