I was having a problem before where my application would crash due to the fourth tab being a nested fragment, so I moved the methods to the main activity to be called on when that tab was selected but now it's giving me the error that the onviewcreated method is never used.
Here is my main activity class:
package com.example.hp_user.shoutfinal28;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity implements OnMapReadyCallback {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from activity_main.xml
setContentView(R.layout.activity_main);
// Locate the viewpager in activity_main.xml
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
// Set the ViewPagerAdapter into ViewPager
viewPager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager()));
}
public void btnShout(View v) {
//allows for label to be changed to shouted once button is pressed
EditText txtInput = (EditText) findViewById(R.id.txtInput);
TextView lblShout = (TextView) findViewById(R.id.lblShout);
lblShout.setText("Shouted! ");
//allows for toast to be displayed once button is clicked
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0);
toast.makeText(MainActivity.this, txtInput.getText() + " Has Been Shouted.", toast.LENGTH_SHORT).show();
}
@Override
public void onViewCreated(View v_maps, Bundle savedInstanceState) {
super.onViewCreated(v_maps, savedInstanceState);
SupportMapFragment supportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.Maps1);
if (supportMapFragment!= null) {
supportMapFragment.getMapAsync(this);
}
}
@Override
public void onMapReady (GoogleMap map) {
map.addMarker(new MarkerOptions()
.position(new LatLng(0, 0))
.title("Marker"));
}
}
Here is my fragmentshouts_maps class:
public class FragmentShouts_Maps extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Get the view from fragment shouts.xml
View root_view = inflater.inflate(R.layout.fragmentshouts_maps, container, false);
return root_view;
}
}