java.lang.NullPointerException:尝试在空对象引用上调用虚方法...

时间:2016-04-09 13:33:57

标签: android exception android-fragments nullpointerexception viewflipper

我在创建ViewFlipper之前创建了一个完美的应用程序。我真的需要使用它,但现在我仍然坚持这个例外。

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

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

}

@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);
}

// 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)

上面的代码给了我这个例外:

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

我认为这部分有问题

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

我该如何解决这个问题?请帮忙。

2 个答案:

答案 0 :(得分:0)

这是你几分钟前提出的问题,为了善待你需要初始化你的viewFlipper onCreate()内部方法,如下所示

@Override
public void onCreate(Bundle savedInstanceState) {
//---------Your extra codes ------
  ViewFlipper viewFlipper = (ViewFlipper) findViewById(R.id.yourViewFlipperId);
//---Then only use this viewFlipper

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


}

答案 1 :(得分:0)

viewFlipper未初始化,因此为NullPointerException。您需要初始化viewFlipper才能使用它。