Google Android 的 API 地点:地图立即消失

Google Places API for Android: Maps vanishing instantly

我正在使用以下代码通过 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_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>

在图像的点击事件中调用上述方法。

代码取自https://developers.google.com/places/android-api/placepicker

上面的代码一执行,屏幕上就会显示一张地图。然而,地图立即消失。

可能是什么问题?

编辑:

完成Activity代码:

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();
        }

    }

检查您的清单文件:

<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