无法在父级或祖先上下文中找到方法“startSearch(View)”

时间:2016-11-21 15:20:44

标签: java android xml

我尝试使用Google Maps Api查找位置。 Xml显示Map,顶部显示EditText-View + Button。

这是xml文件: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>

你可以看到Button_startSearchLocation有一个onClick方法。 我在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文件:

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

}

当我运行应用程序时,我收到此错误:

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'

我想知道为什么xml文件找不到方法?

  • compileSdkVersion 24
  • buildToolsVersion“25.0.0”

  • minSdkVersion 19

  • targetSdkVersion 24

1 个答案:

答案 0 :(得分:0)

Ok sry Guys。愚蠢的错误。 该方法应该是公开的。

JFrame

}