你好,所以我想从我的活动传递一个数组列表的片段传递数组列表,这是我做过什么:空对象引用,而尝试使用接口
FirstActivity:
AdminInterface instanceForInterface;
OnCreate
//
System.out.println(results.size) ; //works fine
instanceForInterface.onDataRecieved(results); // here I am getting the exception
//
public interface AdminInterface {
void onDataRecieved(ArrayList <Result> response);
}
public void setInterface(UserFragment anInterface) {
this.instanceForInterface = anInterface;
}
片段
OnActivityCreated
((FirstActivity) getActivity()).setInterface(this);
@Override
public void onDataRecieved(ArrayList<Result> response) {
processData(response);
}
异常
Attempt to invoke interface method 'void **************.onDataRecieved(java.util.ArrayList)' on a null object reference
我想什么:
我在OnActivityCreated()
解决方案调用此行
instanceForInterface.onDataRecieved(results);
在OnCreate()
的
((FirstActivity) getActivity()).setInterface(this);
在初始化之前,请?
谢谢
的可能的复制[?什么是空指针异常,以及如何解决呢(https://stackoverflow.com/questions/218384/什么是一个nullpointerexception和如何做我修复它) – SripadRaj
放在'onAttach'而不是'OnActivityCreated' –
更好地传递'ArrayList'作为'Bundle' https://stackoverflow.com/问题/ 17436298 /如何传递变量从活动到碎片并传递回 –