java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference. what should i do?

浪尽此生 提交于 2019-12-25 18:36:08

问题


I I have created an app that was working perfectly before I introduces ViewFlipper into it. I really need to use this, but now I'm stuck or this exception.

frag_home.java

public class Frag_home extends Fragment {
private String mParam1;
private String mParam2;
Animation fade_in, fade_out;
ViewFlipper viewFlipper;

private OnFragmentInteractionListener mListener;

public Frag_home() {
    // Required empty public constructor
}


// TODO: Rename and change types and number of parameters
public static Frag_home newInstance(String param1, String param2) {
    Frag_home fragment = new Frag_home();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }

}

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

    fade_in= AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in);
    fade_out=AnimationUtils.loadAnimation(getActivity(),R.anim.fade_out);

    viewFlipper.setAnimation(fade_in);
    viewFlipper.setAnimation(fade_out);

    viewFlipper.setAutoStart(true);
    viewFlipper.setFlipInterval(1000);
    viewFlipper.startFlipping();
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}
public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}

Logcat:

04-09 18:29:02.196 27863-27863/com.example.aastha.trying E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.example.aastha.trying, PID: 27863
                                                                       java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ViewFlipper.setAnimation(android.view.animation.Animation)' on a null object reference
                                                                           at com.example.aastha.trying.Frag_home.onCreate(Frag_home.java:70)
                                                                           at android.support.v4.app.Fragment.performCreate(Fragment.java:1951)
                                                                           at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1029)
                                                                           at android.support.v4.app.BackStackRecord.ensureFragmentsAreInitialized(BackStackRecord.java:1077)
                                                                           at android.support.v4.app.BackStackRecord.beginTransition(BackStackRecord.java:1032)
                                                                           at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:658)
                                                                           at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
                                                                           at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
                                                                           at android.os.Handler.handleCallback(Handler.java:739)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                           at android.os.Looper.loop(Looper.java:145)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5832)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:372)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

The code above gives me this exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ViewFlipper.setAnimation(android.view.animation.Animation)' on a null object reference

How should i resolve this? Please help.


回答1:


In your onCreate() viewFlipper is still null.

Instead, in your onCreateView() you have to get a reference to your ViewFlipper with findViewById and only afterwards do operations on it.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }

    fade_in= AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in);
    fade_out=AnimationUtils.loadAnimation(getActivity(),R.anim.fade_out);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_frag_home, container, false);
    viewFlipper = v.findViewById(R.id.<your_viewflipper_id>);
    viewFlipper.setAnimation(fade_in);
    viewFlipper.setAnimation(fade_out);

    viewFlipper.setAutoStart(true);
    viewFlipper.setFlipInterval(1000);
    viewFlipper.startFlipping();
}

onCreate() is called before your layout is inflated, thus you cannot access views there. Also you need to bind every view via findViewById() in onCreateView(). You should have a look at the fragment lifecycle (figure 2).




回答2:


So, the answer was pretty simple. I only had to initialize the ViewFlipper in onCreateView method:

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

    viewFlipper = (ViewFlipper) v.findViewById(R.id.background);

    fade_in= AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in);
    fade_out=AnimationUtils.loadAnimation(getActivity(),R.anim.fade_out);

    viewFlipper.setAnimation(fade_in);
    viewFlipper.setAnimation(fade_out);

    viewFlipper.setAutoStart(true);
    viewFlipper.setFlipInterval(1000);
    viewFlipper.startFlipping();
    return  v;

} 


来源:https://stackoverflow.com/questions/36517360/java-lang-nullpointerexception-attempt-to-invoke-virtual-method-on-a-null-objec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!