适用于 Android 的 Google Places API:地图立即消失

     2023-04-14     297

关键词:

【中文标题】适用于 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 登录 - 发布与调试

】适用于Android的Google登录-发布与调试【英文标题】:GoogleSigninforAndroid-ReleasevsDebug【发布时间】:2017-05-0220:11:54【问题描述】:所以目前,我正在尝试实现google登录..我已按照developer.android.com上的说明进行操作..我遇到了一个奇... 查看详情

如何设置适用于 Android 的 Google Cloud Messaging?

】如何设置适用于Android的GoogleCloudMessaging?【英文标题】:HowtosetupGoogleCloudMessagingforAndroid?【发布时间】:2012-06-2911:30:51【问题描述】:我正在尝试通过遵循demo来实现GoogleCloudMessagingforAndroid(GCM)但我无法执行某些命令,例如$antwar... 查看详情

适用于 Android 的 Google+ 平台 - NoClassDefFoundError:com.google.android.gms.samples.plus.SignInActivity

】适用于Android的Google+平台-NoClassDefFoundError:com.google.android.gms.samples.plus.SignInActivity【英文标题】:Google+PlatformforAndroid-NoClassDefFoundError:com.google.android.gms.samples.plus.SignInActivity【发布时间】:2013-03-0510:32: 查看详情

为啥使用 KML 数据检索适用于 Android 的 Google 路线不再有效? [复制]

】为啥使用KML数据检索适用于Android的Google路线不再有效?[复制]【英文标题】:WhyretrievingGoogleDirectionsforAndroidusingKMLdataisnotworkinganymore?[duplicate]为什么使用KML数据检索适用于Android的Google路线不再有效?[复制]【发布时间】:2012-07-... 查看详情

获取适用于 Android 的 Google Places API 密钥

】获取适用于Android的GooglePlacesAPI密钥【英文标题】:GettingGooglePlacesAPIkeyforAndroid【发布时间】:2016-03-2710:37:17【问题描述】:在过去的一周里,我一直在尝试为android实现GooglePlacesAPI,Google的开发者页面说我需要一个api密钥,在... 查看详情

从适用于 Android 的 Google 登录迁移到 Firebase 身份验证

】从适用于Android的Google登录迁移到Firebase身份验证【英文标题】:MigratingfromGoogleSign-InforAndroidtoFirebaseAuthentication【发布时间】:2019-05-1515:57:45【问题描述】:目前,我们计划使用GoogleSign-InforAndroid,作为我们的服务器身份验证方... 查看详情

适用于 Android 5.0 的 Google API

】适用于Android5.0的GoogleAPI【英文标题】:GoogleAPIsforAndroid5.0【发布时间】:2014-12-2914:00:01【问题描述】:我开始学习android应用程序开发并尝试从sdkmanager下载必要的平台。当我在API21中选择要下载的包时,有GoogleAPI包,我想知道... 查看详情

适用于 Android 的 Google API 缺少 Games.getGamesAuthToken

】适用于Android的GoogleAPI缺少Games.getGamesAuthToken【英文标题】:GoogleAPIsforAndroidismissingGames.getGamesAuthToken【发布时间】:2016-07-1808:31:14【问题描述】:GoogleAPIsforAndroid的在线reference显示了Games类的公共方法摘要,其中包括:staticPending... 查看详情

适用于 Android 的 Google Places API:地图立即消失

】适用于Android的GooglePlacesAPI:地图立即消失【英文标题】:GooglePlacesAPIforAndroid:Mapsvanishinginstantly【发布时间】:2016-05-3003:38:23【问题描述】:我正在使用以下代码通过GooglePlacesAPIforAndroid在我的应用程序上显示地图:publicvoidshowMa... 查看详情

适用于 Android 的 Google Nearby Publish 在后台

】适用于Android的GoogleNearbyPublish在后台【英文标题】:GoogleNearbyPublishinbackgroundforAndroid【发布时间】:2017-02-1523:01:50【问题描述】:GoogleNearby库仅允许使用BLE在iOS的后台发布(声明为here)。不允许Android具有相同功能的原因是什么... 查看详情

Flurry 适用于没有 Google Play 服务的 Android 设备

】Flurry适用于没有GooglePlay服务的Android设备【英文标题】:FlurryforAndroiddeviceswithoutGooglePlayServices【发布时间】:2015-01-1103:57:10【问题描述】:我们在GooglePlayStore和AmazonAppStore中都分发了一个Android应用程序。因此,某些设备将没有G... 查看详情

适用于 Android 的 Unity Google Play 游戏,Xcode 中出现 +200 错误

】适用于Android的UnityGooglePlay游戏,Xcode中出现+200错误【英文标题】:UnityGooglePlayGamesforAndroid,+200errorsinXcode【发布时间】:2015-02-1514:41:33【问题描述】:在我的Unity项目中,我使用的是适用于Android的GooglePlayGamesPlugin,它在Android平... 查看详情

通过电子邮件安装的适用于 Android 的 Google FCM

】通过电子邮件安装的适用于Android的GoogleFCM【英文标题】:GoogleFCMforAndroidinstalledviaemail【发布时间】:2017-08-0215:37:43【问题描述】:我正在为我的Android应用设置推送通知。该应用程序尚未发布到GooglePlay商店。我正在通过直接在... 查看详情

适用于 Android 的 Google Books API - 未配置访问权限

】适用于Android的GoogleBooksAPI-未配置访问权限【英文标题】:GoogleBooksAPIforAndroid-AccessNotConfigured【发布时间】:2014-01-1611:34:51【问题描述】:我正在使用googlebooksAPIKEYforandroid通过我的android应用程序加载图书数据。但我收到下面列... 查看详情

google 的 Map SDK 计费如何适用于 Android 和 iOS 应用程序?

】google的MapSDK计费如何适用于Android和iOS应用程序?【英文标题】:Howgoogle\'sMapSDKbillingworksforAndroidandiOSapps?【发布时间】:2020-03-0213:47:29【问题描述】:我在我的应用程序中使用适用于iOS和Android的GoogleMapsSDK。以下是我的用例:在... 查看详情

适用于 Android 的 Google 地图我的位置自定义按钮

】适用于Android的Google地图我的位置自定义按钮【英文标题】:Googlemapforandroidmylocationcustombutton【发布时间】:2014-07-1600:01:46【问题描述】:如何更改谷歌地图我的位置默认按钮?我设置了我的位置启用和地图绘制标准图像来查找... 查看详情

是否有任何已发布的实施适用于 Android 的 Google Latitude API 的示例? [关闭]

】是否有任何已发布的实施适用于Android的GoogleLatitudeAPI的示例?[关闭]【英文标题】:ArethereanypublishedexamplesimpementingtheGoogleLatitudeAPIforAndroid?[closed]【发布时间】:2011-03-1401:39:47【问题描述】:是否有任何已发布的示例实现了适用... 查看详情

如何将 Amazon Cognito 与适用于 Android 的 Google Plus 集成?

】如何将AmazonCognito与适用于Android的GooglePlus集成?【英文标题】:HowtointegrateAmazonCognitowithGooglePlusforAndroid?【发布时间】:2015-03-2007:58:19【问题描述】:这是我在这里的第一个问题,所以请温柔一点。作为我大学项目的一部分,... 查看详情