0

I am trying to add a search bar to android maps. The map is added as one of the fragment to the project. The error seems be introduced by this. Any help is appreciated. Thanks in advance.

public class Maps extends Fragment {
    //Initial code

   public void onSearch(View view) {
        EditText location_tf = (EditText) getView().findViewById(R.id.TFaddress);
        String location = location_tf.getText().toString();
        List < Address > addressList = null;

        if (location != null || !location.equals(""))
        Geocoder geocoder = new Geocoder(this);

        try {
            addressList = geocoder.getFromLocationName(location, 1);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    Address address = addressList.get(0);
    LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
    googleMap.animateCamera(CameraUpdateFactory.newLatLng(latLng));
}

xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <EditText android:layout_width="287dp"
            android:layout_height="wrap_content"
            android:id="@+id/TFaddress"/>
        <Button style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"  
            android:layout_height="wrap_content"
            android:text="Search"
            android:id="@+id/button"
            android:layout_gravity="right"
            android:onClick="onSearch" />
        <com.google.android.gms.maps.MapView
            android:id="@+id/map"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </LinearLayout>

Error:

Error:(83, 46) error: incompatible types: MAPS cannot be converted to Context
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output.    
Error:Execution failed for task ':app:compileDebugJava'. Compilation failed; see the compiler error output for details.
antonio
  • 18,044
  • 4
  • 45
  • 61
Chid
  • 19
  • 2
  • 12

1 Answers1

0

According to the documentation, the Geocoder constructor expects a Context, not a Fragment, so this line

Geocoder geocoder = new Geocoder(this);

should be

Geocoder geocoder = new Geocoder(getActivity());

Also, take into account that you may want to add brackets in your if statement:

if (location != null || !location.equals(""))
    Geocoder geocoder = new Geocoder(this);

    try {
        addressList = geocoder.getFromLocationName(location, 1);
    } catch (IOException e) {
        e.printStackTrace();
    }
}
antonio
  • 18,044
  • 4
  • 45
  • 61
  • Thanks for the help. However, after rectifying my code, when I try to run it, the TomCat: java.lang.IllegalStateException: Could not find method onSearch(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'button' – Chid Nov 05 '15 at 13:05
  • Try making your `onSearch(View view)` public: `public onSearch(View view)` – antonio Nov 05 '15 at 13:23
  • My mistake, your `onSearch` method must be on your `Activity`, not your `Fragment` http://stackoverflow.com/questions/21192386/android-fragment-onclick-button-method – antonio Nov 05 '15 at 13:38