支持片段中的PlaceAutocomplete片段

时间:2018-06-04 08:25:28

标签: android android-studio android-fragments google-places-api android-nested-fragment

我在使用google api时非常困难,特别是支持PlaceAutocompleteFragment,所以这是我的问题:

我正在尝试在另一个片段中使用SupportPlaceAutocompleteFragment,我正在使用来自父片段的ChildFragmentManager处理它,所以如果我只是初始化它并运行它它工作正常,但如果我尝试通过使用自定义SupportPlaceAutocompleteFragment接口它的方法(我只需要用setHint定制提示)我的程序崩溃了。 在父片段中我也使用MapFragment,但工作正常。所以这是我的代码:

    public class ParkSharingNavigationFragment extends android.support.v4.app.Fragment implements

OnMapReadyCallback

{
    @Override
    public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    }

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // return inflater.inflate(R.layout.fragment_parksharingnavigationfragment, container, false);

    View v = inflater.inflate(R.layout.fragment_parksharingnavigationfragment, container, false);

    SupportPlaceAutocompleteFragment mSupportAutocompleteFragment =
            new SupportPlaceAutocompleteFragment();

    SupportMapFragment mSupportMapFragment = SupportMapFragment.newInstance();


    mSupportAutocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            // TODO: Get info about the selected place.
            Log.i(TAG, "Place: " + place.getName());
        }

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

    // crea un filtro di ricerca posti personalizzati
    AutocompleteFilter mFilter = new AutocompleteFilter.Builder()
            .setCountry("IT").setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS)
            .build();
    mSupportAutocompleteFragment.setFilter(mFilter);

    final LatLng itNorthEastBorder = new LatLng(6.7499552751, 36.619987291);
    final LatLng itSouthWestBorder = new LatLng(18.4802470232, 47.1153931748);

    // crea l'area del quadrato contenente l'Italia
    LatLngBounds mBounds = new LatLngBounds(itNorthEastBorder, itSouthWestBorder);

    mSupportAutocompleteFragment.setBoundsBias(mBounds);
    mSupportAutocompleteFragment.setHint("Dove vorrai parcheggiare?");

    android.support.v4.app.FragmentTransaction transaction = getChildFragmentManager().
                beginTransaction();

    mSupportMapFragment.getMapAsync(this);
    transaction.add(R.id.mLayout, mSupportAutocompleteFragment);
    transaction.add(R.id.mapView, mSupportMapFragment);
    transaction.commit();

    return v;

}

    @Override
    public void onMapReady(GoogleMap map) {
    GoogleMap mMap = map;
    LatLng colosseo = new LatLng(41.890251, 12.492373);
    LatLng chengdUni = new LatLng(30.656994, 104.080009);
    LatLng uniLab = new LatLng(41.8988052,12.515375);
    mMap.addMarker(new MarkerOptions().position(uniLab).title("Sono qui"));
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(uniLab, 18));


}

}

这是与我的片段类关联的.xml文件:

    <?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout 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:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="parent"
tools:layout_editor_absoluteX="149dp"
tools:layout_editor_absoluteY="188dp">


<com.google.android.gms.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button2"
    android:layout_width="135dp"
    android:layout_height="57dp"
    android:layout_marginStart="8dp"
    android:text="@string/action_cerca_posto"
    app:layout_constraintBaseline_toBaselineOf="@+id/button3"
    app:layout_constraintEnd_toStartOf="@+id/button3"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintHorizontal_chainStyle="spread"
    app:layout_constraintStart_toStartOf="parent" />

<Button
    android:id="@+id/button3"
    android:layout_width="135dp"
    android:layout_height="57dp"
    android:layout_marginBottom="24dp"
    android:layout_marginEnd="8dp"
    android:text="@string/action_lascia_posto"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    app:layout_constraintStart_toEndOf="@+id/button2" />

<FrameLayout
    android:id="@+id/mLayout"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="8dp"
    android:background="@color/cardview_light_background"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">


</FrameLayout>

这是我得到的错误:

    06-04 10:06:30.741 558-558/com.example.dark_.generocityupdated E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dark_.generocityupdated, PID: 558
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setHint(java.lang.CharSequence)' on a null object reference
    at com.google.android.gms.location.places.ui.SupportPlaceAutocompleteFragment.setHint(Unknown Source)
    at com.example.dark_.generocityupdated.Activities.MainActivityFragments.ParkSharingNavigationFragment.onCreateView(ParkSharingNavigationFragment.java:78)
    at android.support.v4.app.Fragment.performCreateView(Fragment.java:2346)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1428)
    at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1759)
    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1827)
    at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:797)
    at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2596)
    at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2383)
    at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245)
    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:703)
    at android.os.Handler.handleCallback(Handler.java:746)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5443)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

我正在寻找解决方案,但我找不到,所以任何帮助或建议都会非常感激。 非常感谢你!

0 个答案:

没有答案