Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Google PlacePicker UI小部件调用在应用程序重启后的几次(4-5次)初始尝试中失败,但在重启后工作正常_Java_Android_Google Api_Google Places Api - Fatal编程技术网

Java Google PlacePicker UI小部件调用在应用程序重启后的几次(4-5次)初始尝试中失败,但在重启后工作正常

Java Google PlacePicker UI小部件调用在应用程序重启后的几次(4-5次)初始尝试中失败,但在重启后工作正常,java,android,google-api,google-places-api,Java,Android,Google Api,Google Places Api,我正在尝试在我的应用程序中实现google PlacePicker小部件。我一直面临的是,在重新启动我的应用程序并单击按钮启动PlacePicker小部件后,它失败了,不幸的是,Google Play服务已经停止。消息出现了几次(4-5次),然后工作正常。我已经浏览了几乎所有与GooglePlacesAPI相关的stackoverflow线程,但所有这些线程都指向API_密钥问题或Android的PlacesAPI被禁用。他们没有一个人谈论我看到的行为 有人能提出可能的问题吗。以下是我在不同文件

我正在尝试在我的应用程序中实现google PlacePicker小部件。我一直面临的是,在重新启动我的应用程序并单击按钮启动PlacePicker小部件后,它失败了,不幸的是,Google Play服务已经停止。消息出现了几次(4-5次),然后工作正常。我已经浏览了几乎所有与GooglePlacesAPI相关的stackoverflow线程,但所有这些线程都指向API_密钥问题或Android的PlacesAPI被禁用。他们没有一个人谈论我看到的行为

有人能提出可能的问题吗。以下是我在不同文件中的代码。我正在测试三星Galaxy S5

AndroidManifest.xml

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

MainActivity.java

public class MainActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener {
    static final int PLACE_PICKER_REQUEST = 2;
    public ListView listView;
    private Activity mainActivity = this;

    private static final int REQUEST_RESOLVE_ERROR = 1001;
    // Unique tag for the error dialog fragment
    private static final String DIALOG_ERROR = "dialog_error";
    // Bool to track whether the app is already resolving an error
    private boolean mResolvingError = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
          .........
          .........
        setContentView(R.layout.main_activity);
          .........
          .........
        setGoogleApiClient();
        listView = (ListView) findViewById(R.id.shopLV);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                if (mGoogleApiClient == null || !mGoogleApiClient.isConnected()) {
                    Toast.makeText(mainActivity, "Enable Location Services or Check your internet connection.", Toast.LENGTH_SHORT).show();
                    return;
                }

                PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();

                try {
                        startActivityForResult(builder.build(mainActivity), PLACE_PICKER_REQUEST);
                } catch (GooglePlayServicesRepairableException
                        | GooglePlayServicesNotAvailableException e) {

                    e.printStackTrace();
                    Toast.makeText(mainActivity, "Google Play Services Unavailable......", Toast.LENGTH_LONG).show();
                }

            }
        });

    }


    private void setGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient
                .Builder(this)
                .enableAutoManage(this, 0, this)
                .addApi(Places.GEO_DATA_API)
                .addApi(Places.PLACE_DETECTION_API)
                .addOnConnectionFailedListener(this)
                .build();
    }

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

        //If the result is from Google API for Places
        if (requestCode == PLACE_PICKER_REQUEST) {
            processPlacePickerRequest(requestCode, resultCode, data);
        }
    }

private void processPlacePickerRequest(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        Place place = PlacePicker.getPlace(mainActivity, data);
        storeName = place.getName().toString();
        storeAddress = place.getAddress().toString();
        googlePlaceID = place.getId().toString();

    } else {
        Toast.makeText(mainActivity, "Please select again!!", Toast.LENGTH_SHORT).show();
    }
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult result) {
    if (mResolvingError) {
        // Already attempting to resolve an error.
        return;
    } else if (result.hasResolution()) {
        try {
            mResolvingError = true;
            result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR);
        } catch (IntentSender.SendIntentException e) {
            // There was an error with the resolution intent. Try again.
            mGoogleApiClient.connect();
        }
    } else {
        // Show dialog using GoogleApiAvailability.getErrorDialog()
        showErrorDialog(result.getErrorCode());
        mResolvingError = true;
    }
}

/*New implemenaton for handling error on connecting to Google API Client*/
// The rest of this code is all about building the error dialog

/* Creates a dialog for an error message */
private void showErrorDialog(int errorCode) {
    // Create a fragment for the error dialog
    ErrorDialogFragment dialogFragment = new ErrorDialogFragment();
    // Pass the error that should be displayed
    Bundle args = new Bundle();
    args.putInt(DIALOG_ERROR, errorCode);
    dialogFragment.setArguments(args);
    //dialogFragment.show(getSupportFragmentManager(), "errordialog");
    dialogFragment.show(getFragmentManager(), "errordialog");
}

/* Called from ErrorDialogFragment when the dialog is dismissed. */
public void onDialogDismissed() {
    mResolvingError = false;
}

/* A fragment to display an error dialog */
public static class ErrorDialogFragment extends DialogFragment {
    public ErrorDialogFragment() { }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Get the error code and retrieve the appropriate dialog
        int errorCode = this.getArguments().getInt(DIALOG_ERROR);
        return GoogleApiAvailability.getInstance().getErrorDialog(
                this.getActivity(), errorCode, REQUEST_RESOLVE_ERROR);
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        ((ShoppingListsMainActivity) getActivity()).onDialogDismissed();
    }
}

@Override
protected void onStart() {
    super.onStart();
    if (mGoogleApiClient != null)
        mGoogleApiClient.connect();
}

@Override
protected void onStop() {
    if (mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
    super.onStop();
}
公共类MainActivity扩展AppCompativeActivity实现GoogleAppClient.OnConnectionFailedListener{
静态最终整数位置选择器请求=2;
公共列表视图列表视图;
私人活动mainActivity=此;
私有静态最终整数请求\u解析\u错误=1001;
//错误对话框片段的唯一标记
私有静态最终字符串DIALOG\u ERROR=“DIALOG\u ERROR”;
//Bool跟踪应用程序是否已在解决错误
私有布尔值mResolvingError=false;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
.........
.........
setContentView(R.layout.main_活动);
.........
.........
setGoogleAppClient();
listView=(listView)findViewById(R.id.shopLV);
setOnItemClickListener(新的AdapterView.OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
如果(mGoogleApiClient==null | |!mGoogleApiClient.isConnected()){
Toast.makeText(mainActivity,“启用定位服务或检查您的internet连接”,Toast.LENGTH_SHORT.show();
返回;
}
PlacePicker.IntentBuilder生成器=新的PlacePicker.IntentBuilder();
试一试{
startActivityForResult(builder.build(主活动)、PLACE\u PICKER\u请求);
}catch(谷歌游戏服务可修复例外
|谷歌PlayServicesNotAvailableException){
e、 printStackTrace();
Toast.makeText(mainActivity,“谷歌播放服务不可用……”),Toast.LENGTH_LONG.show();
}
}
});
}
私有无效SetGoogleAppClient(){
mgoogleapclient=新的Googleapclient
.Builder(本)
.enableAutoManage(this,0,this)
.addApi(Places.GEO_数据_API)
.addApi(Places.PLACE\u DETECTION\u API)
.addOnConnectionFailedListener(此)
.build();
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
//如果结果是从谷歌API的地方
if(requestCode==PLACE\u PICKER\u REQUEST){
processPlacePickerRequest(请求代码、结果代码、数据);
}
}
私有void processPlacePickerRequest(int请求代码、int结果代码、意图数据){
if(resultCode==RESULT\u OK){
Place-Place=PlacePicker.getPlace(主活动,数据);
storeName=place.getName().toString();
storeAddress=place.getAddress().toString();
googlePlaceID=place.getId().toString();
}否则{
Toast.makeText(mainActivity,“请再次选择!!”,Toast.LENGTH_SHORT.show();
}
}
@凌驾
public void onconnection失败(@NonNull ConnectionResult){
如果(mResolvingError){
//已尝试解决错误。
返回;
}else if(result.hasResolution()){
试一试{
mResolvingError=true;
result.startResolutionForResult(这是请求\u解决\u错误);
}catch(IntentSender.sendtintentexe){
//解析意图出错。请重试。
mGoogleApiClient.connect();
}
}否则{
//使用GoogleAppAvailability.getErrorDialog()显示对话框
对话框(result.getErrorCode());
mResolvingError=true;
}
}
/*处理连接到Google API客户端时出错的新实现*/
//这段代码的其余部分都是关于构建错误对话框的
/*为错误消息创建一个对话框*/
专用对话框(int errorCode){
//为错误对话框创建一个片段
ErrorDialogFragment dialogFragment=新的ErrorDialogFragment();
//传递应该显示的错误
Bundle args=新Bundle();
args.putInt(对话框错误,错误代码);
dialogFragment.setArguments(参数);
//显示(getSupportFragmentManager(),“errordialog”);
显示(getFragmentManager(),“errordialog”);
}
/*对话框关闭时从ErrorDialogFragment调用*/
公开无效{
mResolvingError=false;
}
/*显示错误对话框的片段*/
公共静态类ErrorDialogFragment扩展了DialogFragment{
公共ErrorDialogFragment(){}
@凌驾
创建对话框上的公共对话框(Bundle savedInstanceState){
//获取错误代码并检索相应的对话框
int errorCode=this.getArguments().getInt(对话框错误);
返回GoogleAppAvailability.getInstance().getErrorDialog(
此.getActivity()、错误代码、请求\u解决\u错误);
}
@凌驾
公共void onDismiss(对话框接口对话框){
((ShoppingListsMainActivity)getActivity()).OnDialogDisabled();
}
}
@凌驾
受保护的void onStart(){
super.onStart();
if(mGoogleApiClient!=null)
mGoogleApiClient.connec