适用于 Android 的 Google Places API:地图立即消失
Posted
技术标签:
【中文标题】适用于 Android 的 Google Places API:地图立即消失【英文标题】:Google Places API for Android: Maps vanishing instantly 【发布时间】:2016-05-30 03:38:23 【问题描述】:我正在使用以下代码通过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();
布局:
<LinearLayout
android:layout_
android:layout_
android:layout_weight="30"
android:orientation="horizontal"
android:weightSum="100">
<TextView
android:id="@+id/locationLabel"
android:layout_
android:layout_
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_
android:layout_margin="5dp"
android:layout_
android:layout_weight="25"
android:src="@drawable/addlocation"
android:onClick="showMap"/>
</LinearLayout>
在图片点击事件时调用上述方法。
代码取自https://developers.google.com/places/android-api/placepicker
上述代码一执行,屏幕上就会显示一张地图。但是,地图会立即消失。
可能是什么问题?
编辑:
完整的活动代码:
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();
【问题讨论】:
发布整个代码。不只是一个sn-p。负责此行为的相关代码可能在其他地方。 我也是指 Java 代码。不仅仅是 XML。 @IceMAN 出了什么问题? 您确定您发布了正确的代码吗?我看不到任何与地图有关的东西。在 XML 和 Java 中都没有。 @IceMAN 请检查此链接developers.google.com/places/android-api/placepicker。只需要 3 行代码即可显示地图(地点选择器)。 【参考方案1】:检查您的清单文件:
<application>
...
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="YOUR_API_KEY"/>
</application>
检查您是否使用了正确的 API 密钥。
此外,还有其他可用的解决方案 Google Places API for Android Place Picker Does not work
【讨论】:
以上是关于适用于 Android 的 Google Places API:地图立即消失的主要内容,如果未能解决你的问题,请参考以下文章
适用于 Android 的 Google+ 平台 - NoClassDefFoundError:com.google.android.gms.samples.plus.SignInActivity
适用于 Android 的 Google 登录 - 发布与调试
为啥使用 KML 数据检索适用于 Android 的 Google 路线不再有效? [复制]
如何设置适用于 Android 的 Google Cloud Messaging?