一直在尝试实施Communicating with Other Fragments。 selId.onIdSelected(pid.toString())
期间出现错误,并在此处获得空指针异常。我想我不确定接下来要做什么。我确信当我点击当前位置有数据但发送到另一个片段是另一回事。
public static class SectionFragment extends ListFragment {
OnSelectedIdListener selId;
public interface OnSelectedIdListener {
public void onIdSelected(String position);
}
public void IdSelected(String id) {
selId.onIdSelected(id);
}
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
selId = (OnSelectedIdListener) activity;
} catch (ClassCastException e) {
Log.d("error!: ",
String.format(e.toString(), "helow"));
}
}
....
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(pid!=null){
Log.d("if not null here", "pid? "+ pid.toString());
selId.onIdSelected(pid.toString());
}
else{Log.d("Null selected id", ""+position);}
}
}
片段B
public class PlayerFragment extends ListFragment implements
SectionFragment.OnSelectedIdListener{
@Override
public void onIdSelected(String position) {
setSelectedId(position);
}
}
答案 0 :(得分:1)
OnSelectedListener
和idSelected
。null
。Fragment
中实现接口,而在您的代码中,您正在将Activity
作为侦听器进行转换。