Java 在启用片段之前,无法启动自动完成活动

Java 在启用片段之前,无法启动自动完成活动,java,android,maps,Java,Android,Maps,我正在使用谷歌地图的地方API。当我第一次点击片段时使用google的内置UI(AutoCompleteFragment),我得到了UI,但当我第二次点击片段时,我得到了错误 在启用片段之前,无法启动自动完成活动。 其次,当我点击我搜索的地方时,下面没有出现代码。我称之为onCreate方法 private void placeAutoComplete(){ if (!Places.isInitialized()) { Places.initializ

我正在使用谷歌地图的地方API。当我第一次点击片段时使用google的内置UI(AutoCompleteFragment),我得到了UI,但当我第二次点击片段时,我得到了错误

在启用片段之前,无法启动自动完成活动。

其次,当我点击我搜索的地方时,下面没有出现代码。我称之为onCreate方法

 private void placeAutoComplete(){

        if (!Places.isInitialized()) {
            Places.initialize(getApplicationContext(), getString(R.string.google_api_key));
        }


        // Initialize the AutocompleteSupportFragment.
        AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
                getSupportFragmentManager().findFragmentById(R.id.autocomplete_fragment);


        ImageView searchIcon = (ImageView)((LinearLayout)autocompleteFragment.getView()).getChildAt(0);
        searchIcon.setVisibility(View.GONE);
        autocompleteFragment.setHint(getString(R.string.whereToToday));

// Specify the types of place data to return.
        autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME , Place.Field.LAT_LNG));
        Log.e(TAG, "Before entering into things: "  );
// Set up a PlaceSelectionListener to handle the response.
        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
            @Override
            public void onPlaceSelected(Place place) {
                // TODO: Get info about the selected place.
                Log.e(TAG, "Place: " + place.getName() + ", " + place.getId());
                Toast.makeText(MainActivity.this, "we here baby", Toast.LENGTH_SHORT).show();

                LatLng coordinate =   place.getLatLng();
                CameraUpdate location = CameraUpdateFactory.newLatLngZoom(coordinate, 16);
                mMap.animateCamera(location , 1500 , null);
                Marker mMarker = mMap.addMarker(new MarkerOptions()
                        .position(coordinate));



            }

            @Override
            public void onError(Status status) {
                // TODO: Handle the error.
                Log.e(TAG, "An error occurred: " + status);

                Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();
            }
        });




    }
选择位置后,这部分代码根本不起作用

 autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
                @Override
                public void onPlaceSelected(Place place) {
                    // TODO: Get info about the selected place.
                    Log.e(TAG, "Place: " + place.getName() + ", " + place.getId());
                    Toast.makeText(MainActivity.this, "we here baby", Toast.LENGTH_SHORT).show();
                    LatLng coordinate =   place.getLatLng();
                    CameraUpdate location = CameraUpdateFactory.newLatLngZoom(coordinate, 16);
                    mMap.animateCamera(location , 1500 , null);
                    Marker mMarker = mMap.addMarker(new MarkerOptions()
                            .position(coordinate));
                }
                @Override
                public void onError(Status status) {
                    // TODO: Handle the error.
                    Log.e(TAG, "An error occurred: " + status);
                    Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();
                }
            }); 
我的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:elevation="@dimen/elevation"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"

        app:cardCornerRadius="@dimen/roundness"
        android:layout_marginTop="8dp"
        >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/nav_light_black"
                android:padding="14dp"
                android:id="@+id/navigationView"
                >

            </ImageView>

           <!-- <TextView
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:layout_toRightOf="@id/navigationView"
                android:background="@android:color/transparent"
                android:hint="Where to today?"
                android:gravity="center_vertical"
                android:id="@+id/whereTo"
                android:maxLines="1"
                android:layout_marginEnd="50dp"

                android:textColor="@color/colorLighterBlack"
                android:textSize="@dimen/fontForWhereTo"

                >

            </TextView>-->

            <fragment android:id="@+id/autocomplete_fragment"
                android:layout_toRightOf="@id/navigationView"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
                />

        </RelativeLayout>


    </androidx.cardview.widget.CardView>

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MapsActivity" />



    <Button
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentBottom="true"
        android:layout_margin="10dp"
        android:background="@android:color/white"
        android:id="@+id/backToCurrentLocation"
        ></Button>

</RelativeLayout>

删除onActivityResult方法解决了我的问题,如果你想在你的活动中使用onActivityResult,那么根据google文档

如果您正在使用自动完成片段,并且需要覆盖onActivityResult,则必须调用super.onActivityResult,否则片段将无法正常工作。


您也可以放置片段xml吗?我已经用xml更新了我的问题。这是在google文档中编写的。如果您使用的是自动完成片段,并且需要覆盖onActivityResult,则必须调用super.onActivityResult,否则片段将无法正常工作。也试试这个链接。非常感谢。。它解决了我的问题太好了欢迎你