Reputation: 775
I'm working on an Android app with one activity which contains stepper with 3 steps. I've created a layout for each step and insert it programmatically in the scrollview in my activity. One step layout looks like this:
The blue rectangle here is content which will be different in each step. First step should contain a map with the couple of buttons. I thought that it will be easier to make a fragment to work with the map outside the activity and just send data to activity when user finishes work with the map.
I insert each step to the activity with this code:
View step1, step2, step3; // they are global
//Inside onCreate:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
FrameLayout mainView = (FrameLayout) inflater.inflate(R.layout.activity_main, null);
LinearLayout parent = (LinearLayout) mainView.findViewById(R.id.stepper_container);
step1 = inflater.inflate(R.layout.layout_step, null);
parent.addView(step1);
step2 = inflater.inflate(R.layout.layout_step, null);
parent.addView(step2);
step3 = inflater.inflate(R.layout.layout_step, null);
parent.addView(step3);
setContentView(mainView);
I managed to successfully insert fragment with a map to the activity by creating a <FragmentLayout android:id="@+id/fragment_container" ... />"
in it and the following code:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
OfficeMapFragment map = OfficeMapFragment.newInstance();
transaction.add(R.id.fragment_container, map);
transaction.commit();
But I can't do it with new programmatically added views as their elements' id
's are the same and when I change id
parameter in transaction to the id
of the "blue" container in step, app crashes with NullPointerException.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference
OfficeMapFragment
onCreateView method:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_map, container, false);
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this); // It crashes here
return v;
}
How to add the fragment inside one of the programmatically created views?
Upvotes: 0
Views: 343
Reputation: 775
Just solved the problem:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
FrameLayout mainView = (FrameLayout) inflater.inflate(R.layout.activity_main, null);
LinearLayout parent = (LinearLayout) mainView.findViewById(R.id.stepper_container);
step1 = inflater.inflate(R.layout.layout_step, null);
parent.addView(step1);
// Other steps
setContentView(mainView);
FrameLayout container = (FrameLayout) step1.findViewById(R.id.step_content_container);
fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
OfficeMapFragment map = OfficeMapFragment.newInstance();
transaction.add(container.getId(), map);
transaction.commit();
And it works fine (with minor layout bugs).
Upvotes: 1