I have an Activity QuestScreen, which at its creation holds the Fragment FragmentQuest. If i press at the Button, this triggers the next() Method of the Screen activity, leading to replace the FragmentQuest with FragmentAction.
The replacing works so far. The problem is, that after the replace i want to set a TextView in the FragmentAction, to avalue which is generated in the QuestScreen Activity.
My first attempt was to set the TextView outside the onCreatView of FragmentAction. This lead to a Nullpointer exception adressing the TextView.
So far I have looked at: This, That, This and That
The attempt seen below leads to an NullpointerException marked in the code.
In The QuestScreen Class
private void next(){
//The needed value for the TextView is stored in nextTransition
Transition nextTransition;
//Check if an entry is in the list
if(quest.getTransitionsAbleToFire().size() > 0){
nextTransition = quest.getTransitionsAbleToFire().get(0);
fragmentAction = (FragmentAction) fm.findFragmentByTag(TAG_FRAGMENT_ACTION);
// create the fragment and data the first time
if (fragmentAction == null) {
Log.d("QuestScreen onCreate" , "create fAction for first time");
// add the fragment
fragmentAction = (FragmentAction) Fragment.instantiate(this, FragmentAction.class.getName(), null);
fm.beginTransaction().add(fragmentAction, TAG_FRAGMENT_ACTION).commit();
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.flFragmentContainer, fragmentAction);
fragmentTransaction.commit();
}
//Triggers the corresponding activity in fragmentAction, to set the TextView to a value which is stored in nextTransition
fragmentAction.nextAction(nextTransition);
nextTransition.fire();
}
else{
CharSequence text = "Quest END";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(getApplicationContext(), text, duration);
toast.show();
}
}
In the FragmentAction Class
public class FragmentAction extends Fragment {
private TextView txtYourQuest;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// CREATE VIEWS HERE
View layout = inflater.inflate(R.layout.content_action_fragment, container, false);
txtYourQuest = (TextView) layout.findViewById(R.id.txtV_Your_Quest);
return layout;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
public void nextAction(Transition prmTransition){
// --> Here in the enxt Line comes the NPE : java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.app.Activity.findViewById(int)' on a null object reference
FrameLayout container = (FrameLayout) getActivity().findViewById(R.id.flFragmentContainer);
LayoutInflater.from(getActivity())
.inflate(R.layout.content_action_fragment, container, false);
txtYourQuest.setText("Your " + prmTransition.getAction().getPosition() + ". is to:\n "
+ prmTransition.getAction().getActionName());
}