0

I'm trying to create a class Places that creates and menaged the autocomplete fragment. If I create the PlaceSelectionListener in the MainActivity it works, but if I create it in the class the app crash. Where am I doing wrong?

Here the code:

activity_main.xml

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

MainActivity.java (create the Places object)

Places places = new Places(this);

Places.java

public class Places extends AppCompatActivity{

    public Places( Context context ) {

    // Retrieve the PlaceAutocompleteFragment.
    PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
            getFragmentManager().findFragmentById(R.id.autocomplete_fragment);

    // Register a listener to receive callbacks when a place has been selected or an error has
    // occurred.
    autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected( Place place ) {
            Log.e("-----", place.toString());
        }

        @Override
        public void onError( Status status ) {
            Log.e("-----", status.toString());
        }
    });
}}

Error:

06-06 10:05:58.360 20562-20562/com.example.aleclock.streetcamera E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aleclock.streetcamera, PID: 20562
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aleclock.streetcamera/com.example.aleclock.streetcamera.MainActivity}: 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 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
    at android.app.ActivityThread.-wrap12(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6077)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
 Caused by: 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.example.aleclock.streetcamera.classes.Places.<init>(Places.java:35)
    at com.example.aleclock.streetcamera.MainActivity.onCreate(MainActivity.java:53)
    at android.app.Activity.performCreate(Activity.java:6662)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
    at android.app.ActivityThread.-wrap12(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6077) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 
clooock
  • 3
  • 3
  • Why is your `Places` class extending `AppCompatActivity`? – Suleyman Jun 06 '18 at 08:20
  • I don't really know why. I'm I beginner and I find it in some tutorial. Is that an error? – clooock Jun 06 '18 at 08:53
  • There are several, you should find a single tutorial and follow it. Remove the extends part, Places is not an Activity. Also, the reason for your error is that `autocompleteFragment` is null, you are not initialising it properly, try to change that. – Suleyman Jun 06 '18 at 09:00
  • 1
    Thanks a lot, can you advise me a valid tutorial? – clooock Jun 06 '18 at 09:16
  • A good place is always the [documentation](https://developers.google.com/places/android-sdk/autocomplete), but there are others like [this](http://kvenkataprasad.blogspot.com/2017/03/autocomplete-google-places-api-example.html?m=1) one. – Suleyman Jun 06 '18 at 09:18
  • you can follow this link to make Place AutoComplete Place Picker https://stackoverflow.com/a/56477478/5514839 – Rahul Goswami Jul 30 '19 at 06:17

0 Answers0