android - Google地方在1秒后关闭时打开地图

时间:2017-11-09 12:42:34

标签: java android

我通过点击应该打开谷歌地方的按钮创建一个活动的意图,但它再次快速关闭并说没有选择位置,并返回主活动,如果我再次点击则没有任何反应。< / p>

我的api应该没问题,我已经检查过它是连接到api密钥的正确SHA1指纹。

结果代码为2

它在此之前的活动中工作较早,但是当我点击按钮时我需要打开它,现在当我尝试打开这个新活动作为意图它不会工作。

public class MapActivity extends AppCompatActivity {

int PLACE_PICKER_REQUEST = 1;
int status;

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

    status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (status != ConnectionResult.SUCCESS) {
        if (GooglePlayServicesUtil.isUserRecoverableError(status)) {
            GooglePlayServicesUtil.getErrorDialog(status, this,
                    100).show();
        }
    }
    if (status == ConnectionResult.SUCCESS) {
        int PLACE_PICKER_REQUEST = 199;
        PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
        Context context = this;
        try {
            startActivityForResult(builder.build(context), PLACE_PICKER_REQUEST);
        } catch (GooglePlayServicesRepairableException e) {
            e.printStackTrace();
        } catch (GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        }
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    System.out.println("Result code: " + resultCode);
    System.out.println("Request code: " + requestCode);
    if (requestCode == 100) {
        status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    }
    if (requestCode == 199) {

        //process Intent......
        if (data != null) {
            Place place = PlacePicker.getPlace(data, this);
            String toastMsg = String.format("Place: %s", place.getName());
            Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
        } else {
            String toastMsg = ("No location selected.");
            Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show();
        }

    }
}

}

这是从创建地图的新意图的意图

   public void onClick(View view) {
        Intent i = new Intent(this, MapActivity.class);
       startActivity(i);
   }

1 个答案:

答案 0 :(得分:1)

我认为它的发生是因为你使用旧方法的getPlace

尝试通过以下方式交换参数:

Place place = PlacePicker.getPlace(data, this);

Place place = PlacePicker.getPlace(getContext(), data);

更新#2

在开发者控制台中启用Google放置API并将这些行添加到AndroidManifest

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

更新#3

经过一些搜索,看起来有其他人有同样的问题。看看这些链接:

https://github.com/zhangtaii/react-native-google-place-picker/issues/21

https://www.codesd.com/item/google-placepicker-closes-immediately-after-launch-with-result-code-2.html

https://github.com/googlesamples/android-play-places/issues/13