【问题标题】:Google Places API Crashes After LaunchGoogle Places API 启动后崩溃
【发布时间】:2015-08-07 15:35:37
【问题描述】:

我一直在尝试将 Google Places API 集成到我的应用程序中,但似乎无法启动该 API。每次我单击按钮启动 API 时,它都会加载,然后返回到主屏幕。我相信我已遵循 API 文档中的所有步骤,但似乎无法找到问题所在。我的代码如下:

Java 类:

package lgalle19.developements.findplaces;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.common.GooglePlayServicesRepairableException;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlacePicker;


public class PlacePickerActivity extends ActionBarActivity {
private static final int PLACE_PICKER_REQUEST = 1;
private TextView mViewName;
private TextView mViewAddress;
private TextView mViewAttributions;
private Button mPickerButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_place_picker);

    mViewName = (TextView) findViewById(R.id.textView);
    mViewAddress = (TextView) findViewById(R.id.textView2);
    mViewAttributions = (TextView) findViewById(R.id.textView3);
    mPickerButton = (Button) findViewById(R.id.pickerButton);
}

public void onPickButtonClick(View v) {
    // Construct an intent for the place picker
    try {
        PlacePicker.IntentBuilder intentBuilder =
                new PlacePicker.IntentBuilder();
        Intent intent = intentBuilder.build(this);
        // Start the intent by requesting a result,
        // identified by a request code.
        startActivityForResult(intent, PLACE_PICKER_REQUEST);

    } catch (Exception e){
        e.printStackTrace();
    }
}

@Override
protected void onActivityResult(int requestCode,
                                int resultCode, Intent data) {

    if (requestCode == PLACE_PICKER_REQUEST
            && resultCode == Activity.RESULT_OK) {

        // The user has selected a place. Extract the name and address.
        final Place place = PlacePicker.getPlace(data, this);

        final CharSequence name = place.getName();
        final CharSequence address = place.getAddress();
        String attributions = PlacePicker.getAttributions(data);
        if (attributions == null) {
            attributions = "";
        }

        mViewName.setText(name);
        mViewAddress.setText(address);
        mViewAttributions.setText(Html.fromHtml(attributions));

    } else {
        super.onActivityResult(requestCode, resultCode, data);
    }
}
}

主要 XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".PlacePickerActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_marginTop="20dp"
    android:layout_centerHorizontal="true"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView2"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Launch Places API Picker"
    android:id="@+id/pickerButton"
    android:layout_below="@+id/textView2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="50dp"
    android:onClick="onPickButtonClick" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView3"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"/>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView3"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:id="@+id/poweredBy"
    android:src="@drawable/powered_by_google_light"/>


</RelativeLayout>

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="lgalle19.developements.findplaces" >


<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<uses-feature android:glEsVersion="0x00020000" android:required="true" />


<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">

    <activity
        android:name=".PlacePickerActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="MY API KEY"/>
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version"/>
</application>

</manifest>

分级:

apply plugin: 'com.android.application'

android {
compileSdkVersion 22
buildToolsVersion "22.0.1"

defaultConfig {
    applicationId "lgalle19.developements.findplaces"
    minSdkVersion 15
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.google.android.gms:play-services:7.5.0'
}

LogCat:

08-07 13:25:37.053  29527-29527/lgalle19.developements.findplaces E/Zygote﹕ MountEmulatedStorage()
08-07 13:25:37.053  29527-29527/lgalle19.developements.findplaces E/Zygote﹕ v2
08-07 13:25:37.053  29527-29527/lgalle19.developements.findplaces I/libpersona﹕ KNOX_SDCARD checking this for 10245
08-07 13:25:37.053  29527-29527/lgalle19.developements.findplaces I/libpersona﹕ KNOX_SDCARD not a persona
08-07 13:25:37.053  29527-29527/lgalle19.developements.findplaces I/SELinux﹕ Function: selinux_compare_spd_ram, SPD-policy is existed. and_ver=SEPF_SM-G920V_5.0.2 ver=27
08-07 13:25:37.063  29527-29527/lgalle19.developements.findplaces I/SELinux﹕ Function: selinux_compare_spd_ram , priority [1] , priority version is VE=SEPF_SM-G920V_5.0.2_0027
08-07 13:25:37.063  29527-29527/lgalle19.developements.findplaces E/SELinux﹕ [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
08-07 13:25:37.063  29527-29527/lgalle19.developements.findplaces I/art﹕ Late-enabling -Xcheck:jni
08-07 13:25:37.083  29527-29527/lgalle19.developements.findplaces D/TimaKeyStoreProvider﹕ TimaSignature is unavailable
08-07 13:25:37.083  29527-29527/lgalle19.developements.findplaces D/ActivityThread﹕ Added TimaKeyStore provider
08-07 13:25:37.153  29527-29527/lgalle19.developements.findplaces I/InjectionManager﹕ Inside getClassLibPath + mLibMap{0=, 1=}
08-07 13:25:37.163  29527-29527/lgalle19.developements.findplaces I/InjectionManager﹕ Inside getClassLibPath caller
08-07 13:25:37.203  29527-29527/lgalle19.developements.findplaces D/InjectionManager﹕ InjectionManager
08-07 13:25:37.203  29527-29527/lgalle19.developements.findplaces D/InjectionManager﹕ fillFeatureStoreMap lgalle19.developements.findplaces
08-07 13:25:37.213  29527-29527/lgalle19.developements.findplaces I/InjectionManager﹕ Constructor lgalle19.developements.findplaces, Feature store :{}
08-07 13:25:37.213  29527-29527/lgalle19.developements.findplaces I/InjectionManager﹕ featureStore :{}
08-07 13:25:37.273  29527-29527/lgalle19.developements.findplaces D/PhoneWindow﹕ *FMB* installDecor mIsFloating : false
08-07 13:25:37.273  29527-29527/lgalle19.developements.findplaces D/PhoneWindow﹕ *FMB* installDecor flags : -2139029248
08-07 13:25:37.333  29527-29527/lgalle19.developements.findplaces D/Activity﹕ performCreate Call Injection manager
08-07 13:25:37.333  29527-29527/lgalle19.developements.findplaces I/InjectionManager﹕ dispatchOnViewCreated > Target : lgalle19.developements.findplaces.PlacePickerActivity isFragment :false
08-07 13:25:37.353  29527-29561/lgalle19.developements.findplaces D/OpenGLRenderer﹕ Render dirty regions requested: true
08-07 13:25:37.363  29527-29527/lgalle19.developements.findplaces D/PhoneWindow﹕ *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
08-07 13:25:37.363  29527-29527/lgalle19.developements.findplaces D/PhoneWindow﹕ *FMB* isFloatingMenuEnabled return false
08-07 13:25:37.473  29527-29561/lgalle19.developements.findplaces D/libEGL﹕ loaded /vendor/lib64/egl/libGLES_mali.so
08-07 13:25:37.513  29527-29561/lgalle19.developements.findplaces I/OpenGLRenderer﹕ Initialized EGL, version 1.4
08-07 13:25:37.523  29527-29561/lgalle19.developements.findplaces I/OpenGLRenderer﹕ HWUI protection enabled for context ,  &this =0x7fa4c75060 ,&mEglDisplay = 1 , &mEglConfig = -1529904464
08-07 13:25:37.523  29527-29561/lgalle19.developements.findplaces D/OpenGLRenderer﹕ Get maximum texture size. GL_MAX_TEXTURE_SIZE is 8192
08-07 13:25:37.523  29527-29561/lgalle19.developements.findplaces D/OpenGLRenderer﹕ Enabling debug mode 0
08-07 13:25:37.523  29527-29561/lgalle19.developements.findplaces D/mali_winsys﹕ new_window_surface returns 0x3000,  [1440x2560]-format:1
08-07 13:25:37.553  29527-29527/lgalle19.developements.findplaces I/InjectionManager﹕ dispatchCreateOptionsMenu :lgalle19.developements.findplaces.PlacePickerActivity
08-07 13:25:37.553  29527-29527/lgalle19.developements.findplaces I/InjectionManager﹕ dispatchPrepareOptionsMenu :lgalle19.developements.findplaces.PlacePickerActivity
08-07 13:25:37.673  29527-29527/lgalle19.developements.findplaces I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@3d2a49fa time:246449518
08-07 13:25:42.163  29527-29527/lgalle19.developements.findplaces D/ViewRootImpl﹕ ViewPostImeInputStage ACTION_DOWN
08-07 13:25:42.813  29527-29561/lgalle19.developements.findplaces D/OpenGLRenderer﹕ endAllStagingAnimators on 0x7faaf61e00 (RippleDrawable) with handle 0x7f966ee140
08-07 13:25:46.713  29527-29561/lgalle19.developements.findplaces D/mali_winsys﹕ new_window_surface returns 0x3000,  [1440x2560]-format:1
08-07 13:25:46.813  29527-29527/lgalle19.developements.findplaces I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@3d2a49fa time:246458658

我还确保在开发者控制台上启用适用于 Android 的 Google Places API 并创建一个用于存储清单的 API 密钥。此处发布的主题Google PlacePicker Closes Immediately After Launch 似乎与我的问题相似,但我已经将我的 API 密钥添加到我的清单中,所以我的问题似乎有所不同。

非常感谢任何帮助!

【问题讨论】:

  • 好的,谢谢!我已经编辑了原始帖子以包含它和新的日志猫
  • 哈哈我知道这是令人费解的事情。看起来意图正在成功启动,但它只是在启动后一两秒关闭。
  • 是的,我根据您的反馈更新了代码。我更新了代码,因为反馈并没有完全解决问题(尽管它更接近了!)
  • @Heyyou 如果有帮助,应用程序会到达找到我的位置的点,然后退出。屏幕底部的“地点”部分在退出时仍在加载。此外,不幸的是,我在问题中发布的代码是我在制作应用程序时更改的唯一文件。另外,我使用的是 Galaxy S6。
  • @Heyyou 不,我的gradle和开头一样

标签: java android api google-places-api google-places


【解决方案1】:

编辑:

将元数据标记更改为

<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="@string/google_api_key"/>

并将您的 api 密钥作为资源引用而不是硬编码

编辑:

将您的权限置于您的应用程序标签之外。

像这样嵌套:

<manifest .../>
    <permission .../>
    <uses-permission .../>
    <uses-feature .../>
        <application ...>
            <activity .../>
        </application>
</manifest>

尝试将其添加到您的清单中:

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>

这是一个很好的教程:

http://code.tutsplus.com/articles/google-play-services-using-the-places-api--cms-23715


这个问题Google Places API for Android Place Picker Does not work描述了一个类似的问题 answer 给出了一个很好的方向。

确保您启用“Places API for Android”,而不仅仅是“Places API”在开发者控制台中。“Places API for Android”将不会显示 在“APIs & Auth/APIs”下,因为它还不是流行的 API。你 必须使用 API 搜索框搜索它。

如果这能解决您的问题,请告诉我,如果没有,我们可以再看看。

【讨论】:

  • 感谢您的帖子!不幸的是,这不是问题所在。我已经在我的应用程序中启用了适用于 Android API 的 Google Places API。
  • 为您的清单添加权限或确保您的应用程序需要所有权限,然后将 logcat 消息粘贴到显示导致的位置,然后我会尝试解决您的问题。
  • 谢谢你们两位的cmets。我已经更新了原始问题以纳入您的建议。应用程序现在打开 Google Places API(实际上显示地图)然后崩溃。所以应用程序更进一步,但仍会恢复到原始屏幕。
  • 我发布了更新的 logcat,我不相信有任何变化。
  • 你值得奖励!谢谢,@YvetteColomb
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多