Error Could Not Find Method (View), Place Picker Google Api

2
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
get_place = (Button) findViewById(R.id.btnBuscarLugares);
get_place.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();

        Intent intent;
        try {
            intent = builder.build((Activity) getApplicationContext());
            startActivityForResult(intent,PLACE_PICKER_REQUEST);
        } catch (GooglePlayServicesRepairableException e){
            e.printStackTrace();
        } catch (GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        }
    }

    });

protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == PLACE_PICKER_REQUEST){
    if (resultCode == RESULT_OK){
        Place place = PlacePicker.getPlace(data,this);
        String address = String.format("Lugar: %s",place.getAddress());
        get_place.setText(address);
    }
}

}

This is my XML file for the button.

<Button
android:id="@+id/btnBuscarLugares"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#00bab7"
android:layout_marginTop="15dp"
android:text="Buscar Lugares" />

This is the error that I get when I run the application.

  

java.lang.IllegalStateException: Could not find method to search for places (View) in a parent or ancestor Context for android: onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'btnBuscarLugares'

I also tried with this code:

public void buscarlugares(View view) {
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();

Intent intent;
try {
    intent = builder.build((Activity) getApplicationContext());
    startActivityForResult(intent,PLACE_PICKER_REQUEST);
} catch (GooglePlayServicesRepairableException e){
    e.printStackTrace();
} catch (GooglePlayServicesNotAvailableException e) {
    e.printStackTrace();
}
}

protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == PLACE_PICKER_REQUEST){
    if (resultCode == RESULT_OK){
        Place place = PlacePicker.getPlace(data,this);
        String address = String.format("Lugar: %s",place.getAddress());
        get_place.setText(address);
    }
}

}

<Button
android:id="@+id/btnBuscarLugares"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#00bab7"
android:layout_marginTop="15dp"
android:text="Buscar Lugares"
android:onClick="buscarlugares"/>

I still get the same error, I tried to debug the application several times, it does not tell me any error, it just closes it. I also tried to put the code in several applications, I have a MapsActivity.java and it gives me the same error.

    
asked by betterlaylow 04.08.2016 в 21:06
source

1 answer

1

Your activity loads the activity_user.xml layout through setContentView ():

setContentView(R.layout.activity_user);

Ensure that the btnBuscarLugares button is in the activity_user.xml layout:

<Button
android:id="@+id/btnBuscarLugares"
...
...
/>
    
answered by 05.08.2016 в 00:36