2

I want to use PlaceAutocompleteFragment in My Application.

I successfully Implemented PlaceAutocompleteFragment and Its working fine.(In Samsung S4 and HTC Desire).

But When I checked this functionality in Android 5.0 (Mi4i) and Android 5.1.1 (Samsung Galaxy Note 10.1) and Android 6.0 (Moto G Turbo) App is crashed....

I am using below code in Fragment which is implement PlaceSelectionListener.

PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.autocomplete_fragment);
autocompleteFragment.setOnPlaceSelectedListener(this);

@Override
    public void onPlaceSelected(Place place) {
        Toast.makeText(getActivity(), "Place selection : " + place.getAddress(),
                Toast.LENGTH_SHORT).show();
    }

    /**
     * Callback invoked when PlaceAutocompleteFragment encounters an error.
     */
    @Override
    public void onError(Status status) {

        Toast.makeText(getActivity(), "Place selection failed: " + status.getStatusMessage(),
                Toast.LENGTH_SHORT).show();
    }

In XML layout file.

<fragment
      android:id="@+id/autocomplete_fragment"
      android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" />

logcat

FATAL EXCEPTION: main Process: com.organization.project, PID: 11635 java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.location.places.ui.PlaceAutocompleteFragment.setOnPlaceSelectedListener(com.google.android.gms.location.places.ui.PlaceSelectionListener)' on a null object reference at com.organization.project.fragments.FragmentSearchPlace.onCreateView(FragmentSearchPlace.java:39) at android.app.Fragment.performCreateView(Fragment.java:2053) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894) at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) at android.app.BackStackRecord.run(BackStackRecord.java:839) at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1454) at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5234) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)

Added Below Dependencies in Gradle File.

compile 'com.android.support:appcompat-v7:23.3.0'
compile 'com.android.support:design:23.3.0'
compile 'com.makeramen:roundedimageview:2.2.1'
compile "com.android.support:cardview-v7:23.1.1"
compile 'com.google.android.gms:play-services-maps:9.0.1'
compile 'com.google.android.gms:play-services-location:9.0.1'
Chirag Savsani
  • 6,020
  • 4
  • 38
  • 74
  • did you find a solution? I have the same problem when updating from `play-services-location:8.x` to 9.x – Markus Aug 10 '16 at 10:25

2 Answers2

4

From your stack trace, it looks like you're using the Autocomplete fragment nested inside another fragment (FragmentSearchPlace). Maybe using getChildFragmentManager() instead of getFragmentManager() would work.

Community
  • 1
  • 1
AndrewR
  • 10,759
  • 10
  • 45
  • 56
  • `Call requires API level 17 (current min is 15): android.app.Fragment#getChildFragmentManager`. But my min sdk is 15. And I want to give support sdk 15 and Up. – Chirag Savsani May 31 '16 at 05:00
  • Hey thanks for pointing out for getChildFragmentManager(). Its working fine if I change API level. But I want API level 15. Is there any way to do this? – Chirag Savsani May 31 '16 at 05:29
  • See here: http://stackoverflow.com/questions/23640066/getchildfragmentmanager-and-support-libraries – AndrewR Jun 02 '16 at 00:06
0

You don't add a fragment into other fragment by the property "name" in your Layout. You must use:

autocompleteFragment = new SupportPlaceAutocompleteFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();           
transaction.replace(R.id.fragme_autocomplete_destino,autocompleteFragment);
transaction.commit();
User42
  • 970
  • 1
  • 16
  • 27