I have tried many things, such as the 2 references: http://developer.android.com/training/basics/fragments/communicating.html & http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity along with some help from others on StackOverflow and I just cannot get my ListFragment (FragmentA) to send just a simple string to another ListFragment (FragmentB). My app keeps crashing telling me there is a NullPointerException when I click the list in FragmentA going into FragmentB. I have been stuck on this for 2 days now and I just feel like smashing my head against a wall. If anyone knows what is wrong please post code to fix my problem.
MainActivity:
import com.example.fragmentcommunication.FragmentB.OnDataPass;
public class MainActivity extends Activity implements OnDataPass{
...
@Override
public void onCreate(Bundle savedInstanceState) {
...
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
String librarySelected = libraryList[position];
Fragment newFragment = null;
if (librarySelected.equals("Item1")){
newFragment = new FragmentA();
}
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.tab2, newFragment, "FragA");
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.commit();
}
});
}
public void onDataPass(String data) {
// TODO Auto-generated method stub
FragmentB transaction1 = ((FragmentB) getFragmentManager().findFragmentByTag("FragB"));
transaction1.use(data);
}
}
FragmentA:
public class FragmentA extends ListFragment{
...
OnDataPass dataPasser;
public interface OnDataPass{
public void onDataPass(String data);
}
@Override
public void onAttach(Activity a) {
super.onAttach(a);
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
try {
dataPasser = (OnDataPass) a;
} catch (ClassCastException e) {
throw new ClassCastException(a.toString()
+ " must implement OnHeadlineSelectedListener");
}
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String libraryList;
//Get the position the user clicked.
Fragment newFragment = null;
libraryList = l.getItemAtPosition(position).toString();
dataPasser.onDataPass(libraryList);
if(libraryList.equals("Ranged")){
newFragment = new FragmentB();
}
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.tab2, newFragment, "FragB");
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.commit();
}
}
FragmentB:
public class FragmentB extends ListFragment{
...
String getListInfo;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
//Testing to see if the String was passed from Fragment A.
System.out.println(getListInfo);
...
public void use(String data) {
// TODO Auto-generated method stub
this.getListInfo = data;
}
}
Since the primary point behind fragments is that they might not both exist on the screen at the same time (e.g., on
-normalscreens), FragmentA should not be trying to “send just a simple string to” FragmentB. FragmentB may not exist.If an event occurs in FragmentA that may have UI effects beyond FragmentA, FragmentA should tell its hosting activity about the event. The hosting activity can then route the event to the other fragment, either by directly calling a method on it (if the activity hosts FragmentB as well), creating it and adding it to the UI (and passing in the data using a factory method, like
newInstance()), or by callingstartActivity()to launch another activity (if there is insufficient room for FragmentA and FragmentB, and therefore we have a separate activity showing FragmentB).With respect to your existing code,
getListInfoisnullbecause you never assign any value to it.