I am using the following code to show map on my app via Google Places API for Android :
public void showMap(View view){
int PLACE_PICKER_REQUEST = 1;
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
Layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="30"
android:orientation="horizontal"
android:weightSum="100">
<TextView
android:id="@+id/locationLabel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="20dp"
android:layout_marginTop="5dp"
android:layout_weight="75"
android:fontFamily="sans-serif-condensed"
android:textSize="45dp"
/>
<ImageView
android:id="@+id/taskLocation"
android:layout_width="0dp"
android:layout_margin="5dp"
android:layout_height="wrap_content"
android:layout_weight="25"
android:src="@drawable/addlocation"
android:onClick="showMap"/>
</LinearLayout>
On click event of the image the aforementioned method is called.
Code taken from https://developers.google.com/places/android-api/placepicker
As soon as the above code executes, a map is displayed on the screen. However, the map disappears instantly.
What could be the problem?
EDIT:
Complete Activity code:
public class TaskGenerator extends FragmentActivity {
private TaskDataSource dataSource = null;
private String dayName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("TaskGenerator: in onCreate");
Intent intent=getIntent();
setContentView(R.layout.task_generator);
dayName=intent.getCharSequenceExtra("DAY_NAME").toString();
}
public static class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker
final Calendar c = Calendar.getInstance();
int hour = c.get(Calendar.HOUR_OF_DAY);
int minute = c.get(Calendar.MINUTE);
// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getActivity(), this, hour, minute,
DateFormat.is24HourFormat(getActivity()));
}
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
TaskGenerator taskGenerator=(TaskGenerator)getActivity();
taskGenerator.updateTimerLabel(hourOfDay, minute, view.is24HourView());
//dismiss();
}
}
private void updateTimerLabel(int hourOfDay, int minute, boolean is24Hour){
TextView textView=(TextView)findViewById(R.id.timerlabel);
textView.setTextSize(45);
if(hourOfDay<=12) {
textView.setText(hourOfDay + ":" + minute+" AM");
}else{
textView.setText(hourOfDay + ":" + minute+" PM");
}
}
public void showTimePickerDialog(View v) {
DialogFragment newFragment = new TimePickerFragment();
newFragment.show(getFragmentManager(), "timePicker");
}
public void saveTask(View view){
EditText taskTitle = (EditText) findViewById(R.id.taskTitle);
EditText taskDescription = (EditText) findViewById(R.id.taskDescription);
TextView taskTimer=(TextView) findViewById(R.id.timerlabel);
int []dayId= {R.id.sundayToggle, R.id.mondayToggle, R.id.tuesdayToggle, R.id.wednesdayToggle, R.id.thursdayToggle, R.id.fridayToggle, R.id.saturdayToggle};
Map<String, String> dayMap = new HashMap<>();
dayMap.put("MON","Monday");
dayMap.put("TUE","Tuesday");
dayMap.put("WED","Wednesday");
dayMap.put("THURS","Thursday");
dayMap.put("FRI","Friday");
dayMap.put("SAT","Saturday");
dayMap.put("SUN","Sunday");
dataSource = new TaskDataSource(this);
try{
dataSource.open();
}catch(SQLException e){
e.printStackTrace();
}
for(int i=0;i<7;i++){
ToggleButton dayToggle = (ToggleButton)findViewById(dayId[i]);
System.out.println(taskTitle.getText()+" "+taskDescription.getText()+" "+taskTimer.getText()+" "+dayToggle.isChecked()+" "+dayToggle.getTextOn());
if(dayToggle.isChecked()){
Task task = dataSource.createTask(dayMap.get(dayToggle.getTextOn().toString()), taskTitle.getText().toString(),
taskDescription.getText().toString(), taskTimer.getText().toString());
System.out.println(task);
}
}
dataSource.close();
Intent intent = new Intent(this, TaskActivity.class);
intent.putExtra("DAY_NAME",dayName);
startActivity(intent);
finish();
}
public void showMap(View view){
int PLACE_PICKER_REQUEST = 1;
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
try {
startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}