【发布时间】:2017-12-19 08:03:23
【问题描述】:
您好,我正在尝试将我的活动中的数组列表传递给片段,这就是我所做的:
第一个活动:
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
我的想法:
我打这个电话
instanceForInterface.onDataRecieved(results); in OnCreate()
在初始化之前
((FirstActivity) getActivity()).setInterface(this); in OnActivityCreated()
请解决??
谢谢
【问题讨论】:
-
输入
onAttach而不是OnActivityCreated -
最好将
ArrayList传递为Bundlestackoverflow.com/questions/17436298/… -
instanceForInterface对你来说是空的。您应该使用调试来检查它的原因。