FragmentTransaction抛出空引用

时间:2018-04-26 09:53:28

标签: java android android-fragments

我有加载器片段,我正在调用loader fragment中的另一个片段。一切正常,但突然我的片段加载器给了我一个错误。在加载器片段中,我显示进度条5秒,之后将显示另一个片段。

这是我的代码: -

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显示错误: -

 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)

为什么它在片段中显示空对象引用。

2 个答案:

答案 0 :(得分:0)

请勿在{{1​​}}中执行View操作。而是将其移至onCreateView方法。 原因:您的片段尚未设置视图。

此外,替换:

onViewCreated

以下内容:

fragmentManager = getFragmentManager();

答案 1 :(得分:0)

试试这个

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