2016-11-21 1 views
0

ich versuche, eine Position zu finden, mit der Google Maps API. Das Xml zeigt die Map und oben einen EditText-View + Button.Konnte die Methode "startSearch (View)" in einem Elternteil oder Vorfahren nicht finden Kontext

Das ist der XML-Datei: google_maps_layout

<RelativeLayout xmlns:map="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 


    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="textPersonName" 
     android:ems="13" 
     android:id="@+id/editText_locationSearch" 
     android:background="#ffffff" 
     android:layout_alignBottom="@+id/button_startSearchLocation" 

     android:layout_alignTop="@+id/button_startSearchLocation" 
     android:layout_alignParentStart="true" 
     android:hint="Ort einfügen" 
     android:textColorHint="#000000" 
     android:textColor="#000000" 
     /> 

    <fragment 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.SupportMapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     tools:context="group14.event_log.MapsActivity" 
     android:layout_below="@+id/button_startSearchLocation" 
     android:layout_alignParentStart="true" /> 

    <Button 
     android:text="Suche" 
     android:layout_height="wrap_content" 
     android:id="@+id/button_startSearchLocation" 
     android:layout_width="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentEnd="true" 
     android:onClick="startSearch" 
     /> 

</RelativeLayout> 

können Sie sehen, dass es eine onClick Methode für Button_startSearchLocation ist. I definiertem diese Methode in Maps.Activity.java

private void startSearch(View view){ 
    List<android.location.Address> addressList = null; 
    EditText location_editText = (EditText) findViewById(R.id.editText_locationSearch); 
    String location = location_editText.getText().toString(); 

    if(location != null || !location.equals("")){ 
     Geocoder geocoder = new Geocoder(this); 
     try { 
      addressList = geocoder.getFromLocationName(location , 1); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     android.location.Address address = addressList.get(0); 
     LatLng lating = new LatLng(address.getLatitude(), address.getLongitude()); 
     mMap.addMarker(new MarkerOptions().position(lating).title("Marker")); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(lating)); 
    } 
} 

Es ist auch die richtige XML-Datei:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 

private GoogleMap mMap; 

@RequiresApi(api = Build.VERSION_CODES.M) 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.google_maps_layout); 
    // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 
    checkGPS(mMap); 

} 

Wenn ich die Anwendung ausführen bekomme ich diesen Fehler:

java.lang.IllegalStateException: Could not find method startSearch(View) in 
a parent or ancestor Context for android:onClick attribute defined on 
view class android.widget.Button with id 'button_startSearchLocation' 

I möchte wissen, warum die xml-Datei die Methode nicht finden kann?

  • compileSdkVersion 24
  • buildToolsVersion "25.0.0"

  • minSdkVersion 19

  • targetSdkVersion 24

Antwort

0

Ok SRY Typen. Dummer Fehler. Die Methode sollte öffentlich sein.

**public** void startSearch(View view){ 
List<android.location.Address> addressList = null; 
EditText location_editText = (EditText) findViewById(R.id.editText_locationSearch); 
String location = location_editText.getText().toString(); 

if(location != null || !location.equals("")){ 
    Geocoder geocoder = new Geocoder(this); 
    try { 
     addressList = geocoder.getFromLocationName(location , 1); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    android.location.Address address = addressList.get(0); 
    LatLng lating = new LatLng(address.getLatitude(), address.getLongitude()); 
    mMap.addMarker(new MarkerOptions().position(lating).title("Marker")); 
    mMap.moveCamera(CameraUpdateFactory.newLatLng(lating)); 
} 

}

Verwandte Themen