Android + java.lang.NullPointerException:尝试调用接口方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30455806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android + java.lang.NullPointerException: Attempt to invoke interface method
提问by BlueHatCoder
When compiling my code i get this error ("java.lang.NullPointerException: Attempt to invoke interface method") and I still can't figure why.
编译我的代码时出现此错误(“java.lang.NullPointerException:尝试调用接口方法”),但我仍然无法弄清楚原因。
Here is my code:
这是我的代码:
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;
}
}
(here is my error message)
(这是我的错误信息)
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.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)
Thank you in advance.
先感谢您。
回答by Jens
Your variable mCallback
is defined but not initialized. You have to initialize it before you can use it.
您的变量mCallback
已定义但未初始化。您必须先初始化它,然后才能使用它。
回答by Stefan Beike
mCallback
is never initiated.
you have to do something like that:
mCallback
从未启动。你必须做这样的事情:
mCallback = new OnDetailsDefinedListenerImpl();
mCallback.executeOrder(a, b, c, d, e);
or
或者
mCallback = new OnDetailsDefinedListener(){
@Override
void executeOrder(String a, String b, String c, String d, String e){
//doSomething
};
}
回答by Sethbrin
You should add a public method to the FragmentDetails class.
您应该向 FragmentDetails 类添加一个公共方法。
public setOnDetailsDefinedListener(OnDetailsDefinedListener listener) {
mCallback = listener;
}
And just implement it in the caller class. just like how you do when add button listener.
只需在调用者类中实现它。就像添加按钮侦听器时所做的一样。
mFragmentDetails.setOnDetailsDefinedListener(new OnDetailsDefinedListener() {
// You code
})
回答by Miguel Gonzalez
Maybe you need to use onAttach(Activity activity) fragments method
也许你需要使用 onAttach(Activity activity) 片段方法
public void onAttach(Activity activity){
super.onAttach(activity);
if(activity instanceof onSomeListener){
mListener=(onSomeListener) activity;
}
}