【问题标题】:Getting error in google api client with id 0 when used in alertdialog在 alertdialog 中使用 id 为 0 的 google api 客户端出现错误
【发布时间】:2017-03-31 14:23:07
【问题描述】:

我正在使用谷歌的自动位置来获取我在第一次打开对话框时在警报对话框中给出的位置,但当我关闭对话框并再次打开对话框时,它显示给我即使我尝试更改 enableautomanage 并添加了 onstart 和 onpause 方法,但没有任何效果。

谁能告诉我为什么会发生这种情况以及在警报对话框中使用它时如何克服这个错误。

代码:

 final LayoutInflater layoutInflater = LayoutInflater.from(Activity_AddFastEntryDaybook.this);
    final View promptView = layoutInflater.inflate(R.layout.dialog_add_newfarmer, null);
    final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Activity_AddFastEntryDaybook.this);
    alertDialogBuilder.setView(promptView);
    alertDialogBuilder.setCancelable(true);
    mlayout = promptView.findViewById(R.id.relative_view_layout);
    final TextInputLayout ti_farmername = (TextInputLayout) promptView.findViewById(R.id.inputfarname);
    final TextInputLayout ti_farmermobno = (TextInputLayout) promptView.findViewById(R.id.inputfarmobno);
    final TextInputLayout ti_farmerlocation = (TextInputLayout) promptView.findViewById(R.id.inputfarmlocation);
    edt_farmername = (EditText) promptView.findViewById(R.id.farmername);
    edt_farmermobno = (EditText) promptView.findViewById(R.id.farmermobileno);
    final ImageView importcontacts = (ImageView) promptView.findViewById(R.id.img_importcontacts);
    final ImageView btnsave = (ImageView) promptView.findViewById(R.id.img_save);
    final AutoCompleteTextView actv_farmerlocation = (AutoCompleteTextView) promptView.findViewById(R.id.farmerlocation);
    // checkconnection();
    mGoogleApiClient = new GoogleApiClient.Builder(Activity_AddFastEntryDaybook.this)
            .addApi(Places.GEO_DATA_API)
            .enableAutoManage(this, GOOGLE_API_CLIENT_ID, this)
            .addConnectionCallbacks(this)
            .build();
    actv_farmerlocation.setThreshold(1);
    actv_farmerlocation.setOnItemClickListener(mAutocompleteClickListener);
    mPlaceArrayAdapter = new PlaceArrayAdapter(this, android.R.layout.simple_list_item_1,
            BOUNDS_MOUNTAIN_VIEW, null);
    actv_farmerlocation.setDropDownWidth(-1);
    actv_farmerlocation.setAdapter(mPlaceArrayAdapter);

    importcontacts.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            farmercontacts();
        }
    });
    btnsave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String farmer_name = edt_farmername.getText().toString();
            String farmer_mobno = edt_farmermobno.getText().toString();
            String farmer_location = actv_farmerlocation.getText().toString();
            if (farmer_name.length() == 0) {
                edt_farmername.setError("Enter Name");
            } else if (farmer_mobno.length() == 0) {
                edt_farmermobno.setError("MobileNo");
            } else if ((farmer_mobno.length() > 10) && (farmer_mobno.startsWith("6") || farmer_mobno.startsWith("5") || farmer_mobno.startsWith("4") || farmer_mobno.startsWith("3")
                    || farmer_mobno.startsWith("2") || farmer_mobno.startsWith("1") || farmer_mobno.startsWith("0") || farmer_mobno.startsWith("*") || farmer_mobno.startsWith("#") || farmer_mobno.endsWith("*") || farmer_mobno.endsWith("#"))) {
                edt_farmermobno.setError("enter a valid mobile no");
            } else if ((farmer_mobno.length() == 10) && (farmer_mobno.startsWith("6") || farmer_mobno.startsWith("5") || farmer_mobno.startsWith("4") || farmer_mobno.startsWith("3") || farmer_mobno.startsWith("2") || farmer_mobno.startsWith("1") || farmer_mobno.startsWith("0") || farmer_mobno.startsWith("*") || farmer_mobno.startsWith("#") || farmer_mobno.endsWith("*") || farmer_mobno.endsWith("#"))) {
                edt_farmermobno.setError("Invalid mobile no");
            } else if ((farmer_mobno.length() < 10) && farmer_mobno.startsWith("6") || farmer_mobno.startsWith("5") || farmer_mobno.startsWith("4") || farmer_mobno.startsWith("3")
                    || farmer_mobno.startsWith("2") || farmer_mobno.startsWith("1") || farmer_mobno.startsWith("0") || farmer_mobno.startsWith("*") || farmer_mobno.startsWith("#") || farmer_mobno.endsWith("*") || farmer_mobno.endsWith("#")) {
                edt_farmermobno.setError("enter a valid mobile no");

            } else if (farmer_location.length() == 0) {
                actv_farmerlocation.setError("Location");
            } else {
                edt_farmername.setError(null);
                edt_farmermobno.setError(null);
                actv_farmerlocation.setError(null);
                databasehandler.insertFarmer(farmer_name, farmer_mobno, farmer_location);
                alert.dismiss();
                myAdapterfarmer.notifyDataSetChanged();
            }
        }
    });
    alert = alertDialogBuilder.create();
    alert.show();

错误:

java.lang.IllegalStateException: Already managing a GoogleApiClient with id 0
                                                                    at com.google.android.gms.common.internal.zzab.zza(Unknown Source)
                                                                    at com.google.android.gms.internal.zzpk.zza(Unknown Source)
                                                                    at com.google.android.gms.common.api.GoogleApiClient$Builder.zzf(Unknown Source)
                                                                    at com.google.android.gms.common.api.GoogleApiClient$Builder.build(Unknown Source)
                                                                    at codingtown.coconut.daybook.activity.Activity_AddFastEntryDaybook.addnewfarmer(Activity_AddFastEntryDaybook.java:325)
                                                                    at codingtown.coconut.daybook.activity.Activity_AddFastEntryDaybook.access$000(Activity_AddFastEntryDaybook.java:74)
                                                                    at codingtown.coconut.daybook.activity.Activity_AddFastEntryDaybook$5.onClick(Activity_AddFastEntryDaybook.java:265)

【问题讨论】:

    标签: android google-maps android-alertdialog


    【解决方案1】:

    问题不是来自alertdialog,你可以通过添加代码试试这个

    ` mGoogleApiClient .enableAutoManage(this, 0 /* clientId */, this)

    【讨论】:

    • 我已经这样做了,但我仍然遇到同样的错误
    • 请添加@Override public void onDestroy() { super.onDestroy(); mGoogleClient.stopAutoManage(getActivity()); mGoogleClient.disconnect(); }
    • 但是当我使用上面的代码时我正在使用 appcompactactivity 应用程序被关闭并且它显示不能使用片段活动
    猜你喜欢
    • 1970-01-01
    • 2019-06-09
    • 2014-08-22
    • 2012-09-13
    • 2014-09-19
    • 2017-07-28
    • 2013-01-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多