2016-11-21 4 views
0

私はGoogle Maps APIを使用して位置を見つけようとしています。 Xmlにはマップが表示され、上部にはEditText-View + Buttonが表示されます。 XMLファイルザッツ親または祖先でメソッド "startSearch(View)"を見つけることができませんでした。コンテキスト

<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> 

google_maps_layoutあなたはButton_startSearchLocationのためのonClickメソッドがある見ることができます。

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:私は、アプリケーションを実行すると

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); 

} 

が、私はこのエラーを取得する: 私は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)); 
    } 
} 

その右xmlファイルでは、この方法をdefiniedなぜXMLファイルがそのメソッドを見つけることができないのか知りたいですか?

  • compileSdkVersion 24
  • buildToolsVersion "25.0.0"

  • minSdkVersionが19

  • targetSdkVersion 24

答えて

0

OkをSRYみんな。愚かな間違い。 メソッドはパブリックである必要があります。

**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)); 
} 

}

関連する問題