Been trying to implement Communicating with Other Fragments . There are errors during the selId.onIdSelected(pid.toString())
, and getting null pointer exception on this. I think I am not sure on what is the next thing to do. I am sure that when I click the current position has data but sending it to the other fragment is another thing.
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);}
}
}
Fragment B
public class PlayerFragment extends ListFragment implements
SectionFragment.OnSelectedIdListener{
@Override
public void onIdSelected(String position) {
setSelectedId(position);
}
}
OnSelectedListener
and idSelected
respectively. null
. Fragment
whereas in your code, you are casting the Activity
as the listener.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.