Android + java.lang.NullPointerException:尝试调用接口方法

时间:2015-05-26 10:22:24

标签: java android nullpointerexception

编译我的代码时出现此错误(“java.lang.NullPointerException:尝试调用接口方法”),我仍然无法理解原因。

这是我的代码:

    public class FragmentDetails extends Fragment {

        private TextView text1, text2, text3, text4, text5 = null;
        private Button button1 = null;


        OnDetailsDefinedListener mCallback;

        public interface OnDetailsDefinedListener {
            void executeOrder(String a, String b, String c, String d, String e);
        }


        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            rootView = inflater.inflate(R.layout.fragment_details, container, false);

            button1 = (Button) rootView.findViewById(...);

            button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                text1 = getActivity().findViewById(...);
                text2 = getActivity().findViewById(...);
                text3 = getActivity().findViewById(...);
                text4 = getActivity().findViewById(...);
                text5 = getActivity().findViewById(...);

                String a = text1.getText().toString();
                String b = text2.getText().toString();
                String c= text3.getText().toString();
                String d= text4.getText().toString();
                String e= text5.getText().toString();

                //this is where my error appears
                mCallback.executeOrder(a, b, c, d, e);
            }
            return rootView;
        }
    }

(这是我的错误信息)

    05-26 09:39:20.868    2916-2916/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: com.example.blueHatCoder.myapplication, PID: 2916
        java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.blueHatCoder.myapplication.FragmentDetails$OnDetailsDefinedListener.executeOrder(java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String)' on a null object reference
        at com.example.blueHatCoder.myapplication.FragmentDetails$6.onClick(FragmentDetails.java:131)
        at android.view.View.performClick(View.java:4756)
        at android.view.View$PerformClick.run(View.java:19749)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        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:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

提前谢谢。

4 个答案:

答案 0 :(得分:5)

永远不会启动

mCallback。 你必须做那样的事情:

mCallback = new OnDetailsDefinedListenerImpl(); 
mCallback.executeOrder(a, b, c, d, e);

mCallback = new OnDetailsDefinedListener(){
   @Override
   void executeOrder(String a, String b, String c, String d, String e){
     //doSomething
   };
}

答案 1 :(得分:2)

您的变量mCallback已定义但未初始化。您必须先将其初始化,然后才能使用它。

答案 2 :(得分:1)

您应该向FragmentDetails类添加一个公共方法。

public setOnDetailsDefinedListener(OnDetailsDefinedListener listener) {
    mCallback = listener;
}

然后在调用者类中实现它。就像添加按钮监听器时的做法一样。

mFragmentDetails.setOnDetailsDefinedListener(new OnDetailsDefinedListener() {
// You code
})

答案 3 :(得分:0)

也许您需要使用onAttach(活动活动)片段方法

public void onAttach(Activity activity){
    super.onAttach(activity);
    if(activity instanceof onSomeListener){
        mListener=(onSomeListener) activity;
    }
}