2

First,i'd like to say that i'm new in this "Programming world", but I've been struggling with autocompleteFragment by google Places, i'm trying to implement autocompleteFragment in my app, but after entering in my "Welcome Activity" it throws an error and crushes the App:

    Process: com.app.mk.transport, PID: 20295
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.mk.transport/com.app.mk.transport.Welcome}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.libraries.places.widget.AutocompleteSupportFragment.setPlaceFields(java.util.List)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.libraries.places.widget.AutocompleteSupportFragment.setPlaceFields(java.util.List)' on a null object reference
        at com.app.mk.transport.Welcome.setUpLocationPicker(Welcome.java:237)
        at com.app.mk.transport.Welcome.onCreate(Welcome.java:216)
        at android.app.Activity.performCreate(Activity.java:7136)
        at android.app.Activity.performCreate(Activity.java:7127)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

And this is my code:

        if (!Places.isInitialized()) {
            Places.initialize(getApplicationContext(), String.valueOf(R.string.google_api_key));
        }
            autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ADDRESS));


        autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {

            @Override
            public void onPlaceSelected(Place place) {

                if (location_switch.isChecked()) {
                    destination = place.getAddress();
                    destination = destination.replace(" ", "+");
                    getDirection();
                }
            }

            @Override
            public void onError(Status status) {
                // TODO: Handle the error.
                Toast.makeText(Welcome.this, "" + status.toString(), Toast.LENGTH_SHORT).show();
                Log.i(TAG, "An error occurred: " + status);
            }
        });

i'm not sure what I'm doing wrong but if someone could point out what's my mistake i would really appreciate it.

And if i need to show you something else "code" or anything just tell me if i'm missing it out!

Thank you.

Mitkashin
  • 93
  • 8

2 Answers2

4

Hi this issues comes when implementing in fragment .Use getChildFragmentManager instead of getSupportFragmentManager/getFragmentManger(). This will work perfectly.Like this:-

if (!Places.isInitialized()) {
        Places.initialize(getApplicationContext(), "YOUR_API_KEY");
    }


// Initialize the AutocompleteSupportFragment.

AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
        getChildFragmentManager().findFragmentById(R.id.autocomplete_fragment);


autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));


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

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

This worked for me hope this will also worked for you. Thanks....

Rahul Kushwaha
  • 5,473
  • 3
  • 26
  • 30
0

If you are using into activity then Use <androidx.fragment.app.FragmentContainerView> instead < <fragment> tag.

And for add implementation "androidx.fragment:fragment:1.2.0" dependency.

Chintan Joshi
  • 1,207
  • 1
  • 9
  • 17