我正在Android中制作一个简单的地方选择程序,问题是当我点击按钮时,地方选择器打开并在2 - 3秒后自动关闭......请帮忙!
MainActivity.java
package com.example.akshay.myapplication;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
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;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
Button b;
private static final int PLACE_PICKER_REQUEST = 1;
private static final LatLngBounds BOUNDS_MOUNTAIN_VIEW = new LatLngBounds(
new LatLng(37.398160, -122.180831), new LatLng(37.430610, -121.972090));
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button) findViewById((R.id.bOpenMap));
b.setOnClickListener(this);
}
@Override
public void onClick(View v) {
try {
PlacePicker.IntentBuilder intentBuilder =
new PlacePicker.IntentBuilder();
intentBuilder.setLatLngBounds(BOUNDS_MOUNTAIN_VIEW);
Intent intent = intentBuilder.build(getApplicationContext());
startActivityForResult(intent, PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e) {
e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
e.printStackTrace();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PLACE_PICKER_REQUEST) {
if (resultCode == RESULT_OK) {
Place place = PlacePicker.getPlace(data, this);
String toastMsg = String.format("Place: %s", place.getName());
Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
}
}
}
}
activity_main.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open Maps"
android:id="@+id/bOpenMap"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
AndroidManifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.akshay.myapplication" >
<uses-permission android:name="com.example.akshay.myapplication.permission.MAPS_RECEIVE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<uses-permission android:name="android.permission.INTERNET"/>
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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.gms.version"
android:value="@integer/google_play_services_version"/>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="**************API-KEY**********"/>
</application>
</manifest>
logcat的
07-11 22:49:26.314 15108-15108/com.example.akshay.myapplication E/Zygote﹕ MountEmulatedStorage()
07-11 22:49:26.314 15108-15108/com.example.akshay.myapplication E/Zygote﹕ v2
07-11 22:49:26.314 15108-15108/com.example.akshay.myapplication I/SELinux﹕ Function: selinux_compare_spd_ram, SPD-policy is existed. and_ver=SEPF_SM-N910G_5.0.1 ver=22
07-11 22:49:26.314 15108-15108/com.example.akshay.myapplication I/SELinux﹕ Function: selinux_compare_spd_ram , priority [1] , priority version is VE=SEPF_SM-N910G_5.0.1_0022
07-11 22:49:26.314 15108-15108/com.example.akshay.myapplication E/SELinux﹕ [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
07-11 22:49:26.314 15108-15108/com.example.akshay.myapplication I/art﹕ Late-enabling -Xcheck:jni
07-11 22:49:26.314 15108-15108/com.example.akshay.myapplication I/libpersona﹕ KNOX_SDCARD checking this for 10277
07-11 22:49:26.314 15108-15108/com.example.akshay.myapplication I/libpersona﹕ KNOX_SDCARD not a persona
07-11 22:49:26.354 15108-15108/com.example.akshay.myapplication D/TimaKeyStoreProvider﹕ TimaSignature is unavailable
07-11 22:49:26.354 15108-15108/com.example.akshay.myapplication D/ActivityThread﹕ Added TimaKeyStore provider
07-11 22:49:26.444 15108-15108/com.example.akshay.myapplication D/ResourcesManager﹕ creating new AssetManager and set to /data/app/com.example.akshay.myapplication-2/base.apk
07-11 22:49:26.614 15108-15108/com.example.akshay.myapplication D/Activity﹕ performCreate Call secproduct feature valuefalse
07-11 22:49:26.614 15108-15108/com.example.akshay.myapplication D/Activity﹕ performCreate Call debug elastic valuetrue
07-11 22:49:26.664 15108-15193/com.example.akshay.myapplication D/OpenGLRenderer﹕ Render dirty regions requested: true
07-11 22:49:26.704 15108-15193/com.example.akshay.myapplication I/Adreno﹕ EGLInit: QTI Build: 03/02/15, 210b328, I0829b9e471, LA.BF.2.1.05.00.00.203.165
07-11 22:49:26.704 15108-15193/com.example.akshay.myapplication I/OpenGLRenderer﹕ Initialized EGL, version 1.4
07-11 22:49:26.714 15108-15193/com.example.akshay.myapplication I/Adreno﹕ GetNativeFormatFromQctPixelFormat: Invalid qct format (611)
07-11 22:49:26.714 15108-15193/com.example.akshay.myapplication I/Adreno﹕ GetNativeFormatFromQctPixelFormat: Invalid qct format (611)
07-11 22:49:26.714 15108-15193/com.example.akshay.myapplication I/Adreno﹕ GetNativeFormatFromQctPixelFormat: Invalid qct format (611)
07-11 22:49:26.714 15108-15193/com.example.akshay.myapplication I/Adreno﹕ GetNativeFormatFromQctPixelFormat: Invalid qct format (611)
07-11 22:49:26.714 15108-15193/com.example.akshay.myapplication I/Adreno﹕ GetNativeFormatFromQctPixelFormat: Invalid qct format (612)
07-11 22:49:26.714 15108-15193/com.example.akshay.myapplication I/Adreno﹕ GetNativeFormatFromQctPixelFormat: Invalid qct format (612)
07-11 22:49:26.714 15108-15193/com.example.akshay.myapplication I/Adreno﹕ GetNativeFormatFromQctPixelFormat: Invalid qct format (612)
07-11 22:49:26.714 15108-15193/com.example.akshay.myapplication I/Adreno﹕ GetNativeFormatFromQctPixelFormat: Invalid qct format (612)
07-11 22:49:26.714 15108-15193/com.example.akshay.myapplication D/OpenGLRenderer﹕ Get maximum texture size. GL_MAX_TEXTURE_SIZE is 16384
07-11 22:49:26.714 15108-15193/com.example.akshay.myapplication D/OpenGLRenderer﹕ Enabling debug mode 0
07-11 22:49:26.804 15108-15108/com.example.akshay.myapplication I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@2a94005b time:38353037
07-11 22:49:31.494 15108-15108/com.example.akshay.myapplication D/ViewRootImpl﹕ ViewPostImeInputStage ACTION_DOWN
07-11 22:49:31.694 15108-15193/com.example.akshay.myapplication D/OpenGLRenderer﹕ endAllStagingAnimators on 0xafb0b600 (RippleDrawable) with handle 0xaf917bb0
07-11 22:49:32.774 15108-15108/com.example.akshay.myapplication I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@2a94005b time:38359001
07-11 22:51:44.224 15108-15108/com.example.akshay.myapplication D/ViewRootImpl﹕ ViewPostImeInputStage ACTION_DOWN
答案 0 :(得分:0)
我运行了您的代码和配置,并看到了您所看到的相同行为。
在将您的配置与我工作的配置进行比较后,我意识到问题在于您的AndroidManifest.xml中需要更改此设置:
android:name="com.google.android.maps.v2.API_KEY"
对此:
android:name="com.google.android.geo.API_KEY"
我回去并使用这两种配置进行了多次测试,以确保这是造成问题的原因。使用第一个配置,PlacePicker每次都会立即关闭。
使用com.google.android.geo.API_KEY
,每次都能完美运作。
这也是您需要为Places API使用的the documentation州。
使用您的代码进行测试的结果,以及使其运行所需的一项修改:
选择地点后: